场景添加子Layer
bool GamingLayer::init()
{
if(!CCLayer::init())
{
return false;
}
setUpView();
//schedule
this->schedule(schedule_selector(GamingLayer::backgroundScrool),0.1f);
//score layer
ScoreLayer *scoreLy = ScoreLayer::create();
//layer的anchorpoint对position无影响
scoreLy->setPosition(ccp(10.0,CCDirector::sharedDirector()->getWinSize().height-27));
this->addChild(scoreLy);
//time layer
TimeLayer *timeLy = TimeLayer::create();
timeLy->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 109,CCDirector::sharedDirector()->getWinSize().height-27));
this->addChild(timeLy);
return true;
}
schedule实现背景滚动
void GamingLayer::backgroundScrool(float val)
{
CCSprite *bk1 = (CCSprite *)getChildByTag(11);
CCSprite *bk2 = (CCSprite *)getChildByTag(12);
bk1->setPositionY(bk1->getPositionY() - 10.0);
bk2->setPositionY(bk1->getPositionY() + 576);
if(bk2->getPositionY() <= 0)
{
bk1->setPositionY(0.0);
}
}
menu callback 获取父Layer、隐藏自身、启动pause menu、恢复音乐
void PauseLayer::MenuresumeCallBack(CCObject* pSender)
{
CCLayer *farther = (CCLayer *)this->getParent();
//hide pause layer
farther->getChildByTag(99)->setVisible(false);
//enable pause menu
((CCMenu*)farther->getChildByTag(156))->setEnabled(true);
//resume
CCDirector::sharedDirector()->resume();
//background music
if(CocosDenshion::SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
}
切换图片动作
void WarriorLayer::setUpView()
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
//warrior
m_pWarrior = CCSprite::create("ship01.png",CCRectMake(60.0,0.0,60.0,43.0));
m_pWarrior->setPosition(ccp(visibleSize.width/2,visibleSize.height/2));
this->addChild(m_pWarrior);
//animation
CCAnimation *animation = CCAnimation::create();
animation->setDelayPerUnit(0.3f);
animation->addSpriteFrame(CCSpriteFrame::create("ship01.png",CCRectMake(60.0,0.0,60.0,43.0)));
animation->addSpriteFrame(CCSpriteFrame::create("ship01.png",CCRectMake(0.0,0.0,60.0,43.0)));
CCAnimate *animate = CCAnimate::create(animation);
m_pWarrior->runAction(CCRepeatForever::create(animate));
}
CCSpriteBatchNode的使用:
1、CCSpriteBatchNode 中的所有CCSprite只会被渲染1次,因此可以提高游戏的FPS。
2、加入到 CCSpriteBatchNode 中的CCSprite必须使用同一张纹理图。
两种Cocos2d-x触摸事件:
两者的区别:CCSet与CCTouch,一个事件集合一个单个事件。
事件分发的顺序: CCTargetedTouchDelegate->CCStandardTouchDelegate。
CCTargetedTouchDelegate的使用:
(1) 启用触摸事件。
(2) 使用addTargetedDelegate方法注册到触摸事件分发器。
(3) 重载事件回调函数。注意,我们必须在触摸开始事件中针对需要接受的事件返回true以捕捉事件。
(4) 当不再需要接受触摸事件时,使用removeDelegate方法来注销触摸事件的接收。与标准触摸事件相比,不同之处主要在于开始触摸事件需要返回一个代表是否捕捉事件的值。
ccTouchBegan方法返回一个布尔值,表示声明是否要捕捉这个触摸点,只有在此方法中捕捉到的触摸点才会继续引发其他3个事件,否则此触摸点的其他事件都会被忽略。
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
第三个参数表明了是否"吞噬"一个触摸,如果设置为true,一个触摸一旦被捕捉,那么所有优先级更低的接收对象都无法接收到触摸。CCMenu就是一个会"吞噬"且优先级为-128的触摸接收器,由于它的优先级很高,所以菜单按钮总能获得触摸响应。
void WarriorLayer::registerWithTouchDispatcher(void)
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
bool WarriorLayer::ccTouchBegan(CCTouch *pTouches, CCEvent *pEvent)
{
m_pWarrior->stopAllActions();
CCMoveTo *move = CCMoveTo::create(0.5f,pTouches->getLocation());
m_pWarrior->runAction(move);
return true;
}