cocos2d-x建立一個情境並切換

來源:互聯網
上載者:User

在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);//將菜單添加到情境



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.