控制器中的功能并不多,主要是下面这些
//对玩家分数的 *** 作 CC_SYNTHESIZE_Readonly(SaveData *,m_saveData,SaveData); voID update(float tm); //游戏暂停与恢复 voID menuPauseCallback(cocos2d::Ref* pSender); //声音控制 voID menuMusicCallback(cocos2d::Ref* pSender);
下面是这些功能的实现
bool Controller::init(){ if (!Layer::init()) { return false; } bool bRect = false; do { Size winSize = Director::getInstance()->getWinSize(); //从xml文件中读取中文显示出来 auto dictionary = Dictionary::createWithContentsOffile("Fonts/AboutMe.xml"); score_label = Label::createWithTTF( ((__String *)(dictionary->objectForKey("score")))->getCString(),"Fonts/DFPShaoNvW5-GB.ttf",25); score_label->setposition(score_label->getContentSize().wIDth / 2,winSize.height - score_label->getContentSize().height * 2); CC_BREAK_IF(!score_label); this->addChild(score_label); //添加显示分数的标签 m_saveData = SaveData::create(); //这里一定要retain一下saveData,在析构函数中release一下 m_saveData->retain(); auto str = __String::createWithFormat("%d",m_saveData->getscore()); m_score = Label::createWithTTF(str->getCString(),25); m_score->setposition(Point(score_label->getContentSize().wIDth + m_score->getContentSize().wIDth / 2 + 30,winSize.height - score_label->getContentSize().height * 2)); CC_BREAK_IF(!m_score); this->addChild(m_score); //记得更新分数的显示 this->scheduleUpdate(); //游戏声音控制按钮 Sprite *normalMusic = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_musicPause.png")); Sprite *pressedMusic = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_musicPause.png")); pMusicItem = MenuItemSprite::create( normalMusic,normalMusic,NulL,CC_CALLBACK_1(Controller::menuMusicCallback,this)); //游戏暂停按钮 Sprite *normalPause = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_resume_nor.png")); Sprite *pressedPause = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_resume_pressed.png")); pPauseItem = MenuItemSprite::create( normalPause,pressedPause,CC_CALLBACK_1(Controller::menuPauseCallback,this)); Menu *menuPause = Menu::create(pMusicItem,pPauseItem,NulL); menuPause->alignItemsHorizontallyWithpadding(pPauseItem->getContentSize().wIDth/2); menuPause->setposition( Point(winSize.wIDth - pPauseItem->getContentSize().wIDth*2,winSize.height - normalPause->getContentSize().height)); this->addChild(menuPause); } while (0); return true;}//游戏暂停voID Controller::menuPauseCallback(cocos2d::Ref* pSender){ CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("sound/button.mp3"); if (!Director::getInstance()->isPaused()) { // 图标状态设置 pPauseItem->setnormalimage(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_pause_nor.png"))); pPauseItem->setSelectedImage(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_pause_press.png"))); CocosDenshion::SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); // 停止背景音乐 CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects(); // 停止所有的特效 Director::getInstance()->pause(); // 停止所有的动作,敌机飞行,子d前进等 } else { pPauseItem->setnormalimage(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_resume_nor.png"))); pPauseItem->setSelectedImage(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_resume_pressed.png"))); CocosDenshion::SimpleAudioEngine::getInstance()->resumeBackgroundMusic();// 恢复 Director::getInstance()->resume(); // 恢复 }}voID Controller::menuMusicCallback(cocos2d::Ref* pSender){ CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("sound/button.mp3"); if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying()) { // 图标状态设置 pMusicItem->setnormalimage(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_music.png"))); pMusicItem->setSelectedImage(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_music.png"))); CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(); // 停止背景音乐// CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects(); // 停止所有的特效 } else { pMusicItem->setnormalimage(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_musicPause.png"))); pMusicItem->setSelectedImage(Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByname("game_musicPause.png"))); CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/BackgroundMusic.mp3",true);// 恢复// CocosDenshion::SimpleAudioEngine::getInstance()->resumeAllEffects(); }}voID Controller::update(float tm){ auto str = __String::createWithFormat("%d",m_saveData->getscore()); //更新分数和坐标 m_score->setcolor(color3B(255,0)); m_score->setString(str->getCString()); m_score->setpositionX(score_label->getContentSize().wIDth + m_score->getContentSize().wIDth / 2 + 30);}
要实现游戏的暂停功能,可以直接将当前运行的场景暂停,而要实现声音的暂停,通过简单的停止背景音乐、音效却不行。因为不断有新的子d在发射、新的敌机在爆炸等。所以,我使用的方法是 将背景音乐与其他音效绑定。
比如下面子d类中的代码
if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying()) { CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("sound/bullet.wav"); }
虽然功能实现了,不过总感觉方法太水了。。。谁有更好的方式欢迎告知。
总结以上是内存溢出为你收集整理的【cocos2d-x 3.7 飞机大战】 决战南海I (七) 控制器的实现全部内容,希望文章能够帮你解决【cocos2d-x 3.7 飞机大战】 决战南海I (七) 控制器的实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)