獲取子圖層
// 獲取根節(jié)點
auto rootNode = CSLoader::createNode("LaunchScene.csb");
// 1、獲取根節(jié)點的第一級子節(jié)點
cocos2d::ui::ImageView *loadingImage = (cocos2d::ui::ImageView *)rootNode->getChildByName("loadingImage");
// 2、獲取容器下的節(jié)點的方法如下,不分層級
cocos2d::ui::TextField *textField = (cocos2d::ui::TextField *)cocos2d::ui::Helper::seekWidgetByName(rootNode, "textField");
獲取設(shè)備語言
//獲取設(shè)備語言
ccLanguageType mylanguage=CCApplication::sharedApplication()->getCurrentLanguage();
//判斷輸出
switch(mylanguage){
case kLanguageChinese:
//
break;
case kLanguageEnglish:
//
break;
}
場景切換動畫
//創(chuàng)建待切換的場景
auto scene=HelloWorld::createScene();
//給場景包裝動畫特效
auto transition=TransitionZoomFlipY::create(1.0f,scene);
//運(yùn)用導(dǎo)演類來進(jìn)行切換場景
Director::getInstance()->replaceScene(transition);
下面列舉下場景切換的動畫效果,可以參考上面的代碼來運(yùn)用各個特效,在自己的游戲中適當(dāng)?shù)倪\(yùn)用特效可以起到很好的效果。
//慢慢淡化到另一場景
TransitionCrossFade::create(時間,目標(biāo)場景);
//本場景變暗消失后另一場景慢慢出現(xiàn)
TransitionFade::create(時間,目標(biāo)場景);
//本場景右上角到左下角方塊消失到另一場景
TransitionFadeBL::create(時間,目標(biāo)場景);
//本場景從上到下橫條消失到另一場景
TransitionFadeDown::create(時間,目標(biāo)場景);
//本場景左下角到右上角方塊消失到另一場景
TransitionFadeTR::create(時間,目標(biāo)場景);
//本場景從下到上橫條消失到另一場景
TransitionFadeUp::create(時間,目標(biāo)場景);
//本場景翻轉(zhuǎn)消失到另一場景(斜上方)
TransitionFlipAngular::create(時間,目標(biāo)場景,樣式 );
//本場景翻轉(zhuǎn)消失到另一場景(X軸)
TransitionFlipX::create(時間,目標(biāo)場景,樣式);
//本場景翻轉(zhuǎn)消失到另一場景(Y軸)
TransitionFlipY::create(時間,目標(biāo)場景);
//本場景跳動消失后另一場景跳動出現(xiàn)
TransitionJumpZoom::create(時間,目標(biāo)場景);
//另一場景由整體從下面出現(xiàn)
TransitionMoveInB::create(時間,目標(biāo)場景);
//另一場景由整體從左面出現(xiàn)
TransitionMoveInL::create(時間,目標(biāo)場景);
//另一場景由整體從上面出現(xiàn)
TransitionMoveInT::create(時間,目標(biāo)場景);
//另一場景由整體從右面出現(xiàn)
TransitionMoveInR::create(時間,目標(biāo)場景);
//翻頁切換,bool為true是向前翻。
TransitionPageTurn::create(時間,目標(biāo)場景,bool);
//本場景從左到右消失同時另一場景出現(xiàn)
TransitionProgressHorizontal::create(時間,目標(biāo)場景);
//本場景從中間到四周消失同時另一場景出現(xiàn)
TransitionProgressInOut::create(時間,目標(biāo)場景);
//本場景從四周到中間消失同時另一場景出現(xiàn)
TransitionProgressOutIn::create(時間,目標(biāo)場景);
//本場景逆時針消失到另一場景
TransitionProgressRadialCCW::create(時間,目標(biāo)場景);
//本場景順時針消失到另一場景
TransitionProgressRadialCW::create(時間,目標(biāo)場景);
//本場景從上到下消失同時另一場景出現(xiàn)
TransitionProgressVertical::create(時間,目標(biāo)場景);
//本場景旋轉(zhuǎn)消失后另一場景旋轉(zhuǎn)出現(xiàn)
TransitionRotoZoom::create(時間,目標(biāo)場景);
//本場景縮小切換到另一場景放大
TransitionShrinkGrow::create(時間,目標(biāo)場景);
//本場景向上滑動到另一場景
TransitionSlideInB::create(時間,目標(biāo)場景);
//本場景向右滑動到另一場景
TransitionSlideInL::create(時間,目標(biāo)場景);
//本場景向左滑動到另一場景
TransitionSlideInR::create(時間,目標(biāo)場景);
//本場景向下滑動到另一場景
TransitionSlideInT::create(時間,目標(biāo)場景);
//本場景三矩形上下消失后另一場景三矩形上下出現(xiàn)
TransitionSplitCols::create(時間,目標(biāo)場景);
//本場景三矩形左右消失后另一場景三矩形左右出現(xiàn)
TransitionSplitRows::create(時間,目標(biāo)場景);
//本場景小方塊消失到另一場景
TransitionTurnOffTiles::create(時間,目標(biāo)場景);
//本場景翻轉(zhuǎn)消失到另一場景(斜上方)
TransitionZoomFlipAngular::create(時間,目標(biāo)場景,樣式);
//本場景翻轉(zhuǎn)消失到另一場景(X軸)
TransitionZoomFlipX::create(時間,目標(biāo)場景,樣式);
//本場景翻轉(zhuǎn)消失到另一場景(Y軸)
TransitionZoomFlipY::create(時間,目標(biāo)場景,樣式);
添加動畫
// 申明一個數(shù)組
Vector<SpriteFrame*>frames(8);
// 幀緩存
auto cache = SpriteFrameCache::getInstance();
// 獲取幀
auto frame1 = cache->getSpriteFrameByName("mainMatchman/mainMatchman_01.png");
frames.pushBack(frame1);
// 獲取幀
auto frame2 = cache->getSpriteFrameByName("mainMatchman/mainMatchman_02.png");
frames.pushBack(frame2);
// 創(chuàng)建動畫
auto animation = Animation::createWithSpriteFrames(frames,1.0f);
matchman_1->runAction(RepeatForever::create(Animate::create(animation)));
注意:Animation::createWithSpriteFrames(frames,1.0f);方法里的1.0f是間隔時間,一定要加上,系統(tǒng)的方法沒有申明該屬性,否則不會生成動畫。
按鈕添加事件
在.h 文件中申明函數(shù)
void musicBtnAction(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type);
在.cpp 文件中添加事件
musicBtn->addTouchEventListener(CC_CALLBACK_2(MainScene::musicBtnAction, this));
實現(xiàn)函數(shù)方法
void MainScene::musicBtnAction(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
switch (type) {
case cocos2d::ui::Widget::TouchEventType::BEGAN:
break;
case cocos2d::ui::Widget::TouchEventType::MOVED:
break;
case cocos2d::ui::Widget::TouchEventType::ENDED:
break;
case cocos2d::ui::Widget::TouchEventType::CANCELED:
break;
default:
break;
}
}
3.0版本,點擊button,點擊事件響應(yīng)兩次,原因是3.0版本的點擊按鈕事件被集合在一起了,點擊抬起移動都會觸發(fā),需要自己來判斷觸發(fā)類型。
多點觸摸
在.h文件申明函數(shù)
void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
void onTouchesCancelled(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *event);
在.cpp文件實現(xiàn)
// 創(chuàng)建一個事件監(jiān)聽器類型為AllAtOnce的多點觸摸
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(GameScene::onTouchesBegan,this);
listener->onTouchesMoved = CC_CALLBACK_2(GameScene::onTouchesMoved,this);
listener->onTouchesEnded = CC_CALLBACK_2(GameScene::onTouchesEnded,this);
listener->onTouchesCancelled = CC_CALLBACK_2(GameScene::onTouchesCancelled,this);
// 添加監(jiān)聽器
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
//觸摸事件開始,手指按下時
void GameScene::onTouchesBegan(const std::vector<Touch*>& touches, cocos2d::Event *event)
{
for(auto &item : touches)
{
auto touch = item;
auto pos1 = touch->getLocation();
auto pos2 = touch->getLocationInView();
auto pos3 = Director::getInstance()->convertToUI(pos2);
log("pos1 x: %f, y: %f",pos1.x,pos1.y);
log("pos2 x: %f, y: %f",pos2.x,pos2.y);
log("pos3 x: %f, y: %f",pos3.x,pos3.y);
}
}
//觸摸移動事件,也就是手指在屏幕滑動的過程
void GameScene::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event)
{
log("TouchTest onTouchesMoved");
}
//觸摸事件結(jié)束,也就是手指松開時
void GameScene::onTouchesEnded(const std::vector<Touch*>& touches, cocos2d::Event *event)
{
log("TouchTest onTouchesEnded");
}
//打斷觸摸事件,一般是系統(tǒng)層級的消息,如來電話,觸摸事件就會被打斷
void GameScene::onTouchesCancelled(const std::vector<Touch*>& touches, cocos2d::Event *event)
{
log("TouchTest onTouchesCancelled");
}
switch語句
case分支下如果過出現(xiàn)兩條以上(包含兩條)的語句。就要用大括號{} 括起來
e.g.:case 0:
{
nsstring *ss = @"www";
NSString *s = @"123";
}
3D震動效果
不能直接讓sprite執(zhí)行GridActions。你需要用一個NodeGrid來承載sprite
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
//this->addChild(sprite, 0);
auto nodegrid = NodeGrid::create();
nodegrid->addChild(sprite);
this->addChild(nodegrid);
//run Shaky3D action
auto shaky3D = Shaky3D::create(5, CCSize(10, 10), 15, false);
nodegrid->runAction(shaky3D);