1、頭檔案中
#include <iostream>
#include "cocos2d.h"
#include "Scence.h"
#include "DefineHeader.h"
#include"SelectSceneLayer.h"
USING_NS_CC;
class StartLayer:public CCLayer
{
public:
virtual bool init();
SCENE_FUNC(StartLayer);
CREATE_FUNC(StartLayer);
virtual void onEnter();
virtual void onExit();
//移除鳥
void removeBirds(CCSprite *bird);
/
//加載遊戲标題
void addGameTitle();
//加載遊戲的背景
void addGameBack();
//添加按鈕
void addButton();
//加入小鳥
void addBird();
//背景層移動
void moveBackground();
//添加背景音樂菜單
void addMenuSound();
// //點選聲音菜單開關
void SoundClick();
bool isSound;
//選擇關卡
void SelectSceneClick();
void ExitClick();
void HelpClick();
private:
CCSprite *bg1;
CCSprite *bg2;
// bool isPlay;
};
2、實作檔案中:
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
bool StartLayer::init()
{
if (!CCLayer::init())
{
return false;
}
isSound = true;
//添加背景音樂
SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_background, true);
//标題
this->addGameTitle();
//添加遊戲的背景
this-> addGameBack();
//背景移動
this->schedule(schedule_selector(StartLayer::moveBackground), 0.1);
//按鈕
this->addButton();
this->addMenuSound();
// 添加跳動的鳥
this->schedule(schedule_selector(StartLayer::addBird), 4.0f);
//添加粒子的效果
int X=arc4random()%400+50;
int Y=arc4random()%300+50;
CCParticleGalaxy *a=CCParticleGalaxy::create();
a->setPosition(ccp(X,Y));
this->addChild(a);
a->setAutoRemoveOnFinish(true);
return true;
}
//添加字型
void StartLayer::addGameTitle()
{
CCLabelTTF *label=CCLabelTTF::create("憤怒的小鳥", "Arial", 40);
label->setPosition(ccp(WINSIZE.width/2, WINSIZE.height-50));
this->addChild(label,1);
}
//添加背景
void StartLayer::addGameBack()
{
//背景1
bg1 = CCSprite::create(ENTER_backSprite);
bg1->setPosition(ccp(WINSIZE.width/2,WINSIZE.height/2));
this->addChild(bg1);
//背景2
bg2 = CCSprite::create(ENTER_backSprite);
bg2->setPosition(ccp(WINSIZE.width/2+WINSIZE.width,WINSIZE.height/2));
this->addChild(bg2);
}
//添加按鈕
void StartLayer::addButton()
{
CCMenuItemImage *button1=CCMenuItemImage::create(PLAYBTN, PLAYBTN);
button1->setScaleY(0.7);
button1->setTarget(this, menu_selector(StartLayer::SelectSceneClick));
CCMenu *playMenu=CCMenu::create(button1,NULL);
playMenu->setPosition(ccp(WINSIZE.width/2, WINSIZE.height/2+60));
this->addChild(playMenu);
//添加退出按鈕
CCMenuItemImage *button2=CCMenuItemImage::create(EXITGAME, EXITGAME);
//button2->setTarget(this, menu_selector(StartLayer::SelectSceneClick));
button2->setTarget(this, menu_selector(StartLayer::ExitClick));
button2->setScaleX(0.9);
button2->setScaleY(1.8);
CCMenu *playMenu1=CCMenu::create(button2,NULL);
playMenu1->setPosition(ccp(WINSIZE.width/2, WINSIZE.height/2));
this->addChild(playMenu1);
//添加幫助按鈕
CCMenuItemImage *button3=CCMenuItemImage::create(HELPGAME, HELPGAME);
button3->setScaleX(0.8);
button3->setScaleY(1.1);
button3->setTarget(this, menu_selector(StartLayer::HelpClick));
CCMenu *help=CCMenu::create(button3,NULL);
help->setPosition(ccp(WINSIZE.width/2, WINSIZE.height/2-60));
this->addChild(help);
}
//退出與幫助菜單的事件
void StartLayer::ExitClick()
{
CCLog("------------Exit-----------");
CCDirector::sharedDirector()->end();
}
void StartLayer::HelpClick()
{
CCLog("------------Help-----------");
}
//添加音樂菜單
void StartLayer::addMenuSound()
{
CCMenuItemImage *soundItem_on=CCMenuItemImage::create(SOUNDITEM_on,SOUNDITEM_on);
CCMenuItemImage *soundItem_off=CCMenuItemImage::create(SOUNDITEM_off,SOUNDITEM_off);
CCMenuItemToggle *toggle=CCMenuItemToggle::createWithTarget(this, menu_selector(StartLayer::SoundClick), soundItem_on,soundItem_off,NULL);
CCMenu *menu=CCMenu::create(toggle,NULL);
menu->setPosition(ccp(50, 50));
this->addChild(menu,1);
}
//選擇關卡
void StartLayer::SelectSceneClick()
{
CCDirector::sharedDirector()->replaceScene(SelectSceneLayer::scene());
CCLog("ughiuerhgiuaerhgiuaeghieugh");
}
//開關聲音的事件
void StartLayer::SoundClick()
{
if( isSound)
{
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
isSound=false;
}
else
{
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
isSound=true;
}
}
//背景層的移動
void StartLayer::moveBackground()
{
if (bg1->getPositionX() == WINSIZE.width/2)
{
if (bg2->getPositionX() == -WINSIZE.width/2)
{
bg2->setPosition(ccp(WINSIZE.width/2+WINSIZE.width,WINSIZE.height/2));
}
CCMoveTo *moveTo1 = CCMoveTo::create(10.0f, ccp(-WINSIZE.width/2, WINSIZE.height/2));
CCMoveTo *moveTo2 = CCMoveTo::create(10.0f, ccp(WINSIZE.width/2, WINSIZE.height/2));
bg1->stopAllActions();
bg2->stopAllActions();
bg1->runAction(moveTo1);
bg2->runAction(moveTo2);
}
if (bg1->getPositionX() == -WINSIZE.width/2)
{
CCMoveTo *moveTo1 = CCMoveTo::create(10.0f, ccp(-WINSIZE.width/2, WINSIZE.height/2));
CCMoveTo *moveTo2 = CCMoveTo::create(10.0f, ccp(WINSIZE.width/2, WINSIZE.height/2));
bg1->setPosition(ccp(WINSIZE.width/2+WINSIZE.width,WINSIZE.height/2));
bg1->stopAllActions();
bg2->stopAllActions();
bg1->runAction(moveTo2);
bg2->runAction(moveTo1);
}
}
//加入小鳥
void StartLayer::addBird()
{
int flg=rand()%2;
CCSprite *bird;
if(flg==0)
{
bird=CCSprite::create( Yellowbird1);
CCLog("-------aaaaaaaa-------");
}
else
{
bird=CCSprite::create(Bird3);
CCLog("-------bbbbbbbb-------");
}
int X1=rand()%120+50;
int X2=rand()%120+250;
int height=rand()%40+40;
bird->setPosition(ccp(X1, 32));
bird->setScale(0.3);
this->addChild(bird);
/
float t=rand()%2+0.3f;
CCJumpTo *jumpTo = CCJumpTo::create(t, ccp(X2,32), height, 1);
CCCallFuncN *callFuncN = CCCallFuncN::create(this, callfuncN_selector( StartLayer::removeBirds));
CCSequence *sequence = CCSequence::create(jumpTo,CCDelayTime::create(2.0f),callFuncN,NULL);
bird->runAction(sequence);
}
//移除鳥
void StartLayer::removeBirds(CCSprite *bird)
{
bird->removeFromParent();
}
void StartLayer::onEnter()
{
CCLayer::onEnter();
}
void StartLayer::onExit()
{
this->unscheduleAllSelectors();
CCLayer::onExit();
}