cocos2d-x新建一个场景并切换

来源:互联网 发布:阿里云可以用来做什么 编辑:程序博客网 时间:2024/05/15 10:03

在cocos2d-x分有场景,层,精灵的概念,一开始首先来新建一个场景

在项目class中添加如下代码

//fishscene.h

#ifndef __FISH_H_
#define __FISH_H__


#include "cocos2d.h"


#include "SimpleAudioEngine.h"


class fish : public cocos2d::CCLayer//新建的类fish
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  //场景的初始化


    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();//场景的创建
    
    // a selector callback
    virtual void menuCloseCallback(CCObject* pSender);//场景中的退出按钮


    // implement the "static node()" method manually
    LAYER_NODE_FUNC(fish);
};


#endif  // __HELLOWORLD_SCENE_H__

在fishscene.cpp中添加声明函数的实现

//fishscene.cpp

#include "fishscene.h"


using namespace cocos2d;


CCScene* fish::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::node();
        CC_BREAK_IF(! scene);


        // 'layer' is an autorelease object
        fish *layer = fish::node();
        CC_BREAK_IF(! layer);


        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);


    // return the scene
    return scene;
}


// on "init" you need to initialize your instance
bool fish::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////


        CC_BREAK_IF(! CCLayer::init());


        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////


        // 1. Add a menu item with "X" image, which is clicked to quit the program.


        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
            "closeNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(fish::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);


        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));


        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);


        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);


        // 2. Add a label shows "Hello World".


        // Create a label and initialize with string "Hello World".
        CCLabelTTF* pLabel = CCLabelTTF::labelWithString("qixianghui", "Thonburi", 64);//讲字换为qixianghui
        CC_BREAK_IF(! pLabel);


        // Get window size and place the label upper. 
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 20));


        // Add the label to HelloWorld layer as a child layer.
        this->addChild(pLabel, 1);


        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::spriteWithFile("fish7.png");//用fish7作为场景
        CC_BREAK_IF(! pSprite);


        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));


        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);


        bRet = true;
    } while (0);


    return bRet;
}


void fish::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

此时 还需要在原先的helloworld场景中添加一个按钮 作为场景切换

在hellowrld.h中添加如下声明

virtual void menuNextScene(CCObject* pSender);

在heeloworld.cpp中添加实现

void HelloWorld::menuNextScene(CCObject* pSender)
{
CCScene *scene=fish::scene();
CCDirector::sharedDirector()->replaceScene(scene);
}

然后将按钮添加之场景中

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
            "CloseNormal.png",//点击前的图片
            "CloseSelected.png",//点击后的图片
            this,
            menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
 // Create a "nextscene" menu item with close icon.
  CCMenuItemImage *pNextItem = CCMenuItemImage::itemFromNormalImage(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuNextScene));
        CC_BREAK_IF(! pNextItem);


        // Place the menu item bottom-right conner.
pNextItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, CCDirector::sharedDirector()->getWinSize().height - 20));//设置按钮的位置
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, pNextItem,NULL);//将按钮加入到菜单
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);


        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);//将菜单添加到场景



原创粉丝点击