Cocos2d-X 愤怒的小鸟的实现------开始界面(二)

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();

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值