Cocos2d-x3.2 场景的生命周期

//GameScene.h

#include "cocos2d.h"

USING_NS_CC;

class GameScene : public cocos2d::Layer
{
public:
    GameScene(void);
    ~GameScene(void);
    
    static cocos2d::Scene* createScene();
    
    virtual bool init();
    
    virtual void onEnter();
    virtual void onEnterTransitionDidFinish();
    virtual void onExit();
    virtual void onExitTransitionDidStart();
    
    void menuCallback(Ref* pSender);
    
    CREATE_FUNC(GameScene);
    
private:        //注意不能用auto关键字
    Size size;
    Sprite *sprite;
};



//GameScene.cpp

//cocos2d: GameScene constructor
//cocos2d: GameScene onEnter
//cocos2d: GameScene onEnterTransitionDidFinish
//cocos2d: HelloWorld constructor
//cocos2d: GameScene onExitTransitionDidStart
//cocos2d: GameScene onExit
//cocos2d: GameScene destructor
//cocos2d: HelloWorld onEnter
//cocos2d: HelloWorld onEnterTransitionDidFinish
//cocos2d: HelloWorld onExitTransitionDidStart
//cocos2d: HelloWorld onExit
//cocos2d: HelloWorld destructor


#include "GameScene.h"
#include "HelloWorldScene.h"

USING_NS_CC;

GameScene::GameScene(void)
{
    log("GameScene constructor");
}

GameScene::~GameScene(void)
{
    log("GameScene destructor");
}

cocos2d::Scene* GameScene::createScene()
{
    auto scene = Scene::create();   //创建一个场景
    auto layer = GameScene::create();   //创建一个图层
    scene->addChild(layer);
    return scene;
}

//初始化当前的图层
bool GameScene::init()
{
    if(!Layer::init())      //初始化父类
        return false;
    
    //获取屏幕大小
    size = Director::getInstance()->getVisibleSize();
    //auto size = Director::getInstance()->getWinSize();
    //添加一个图片精灵
    sprite = Sprite::create("OnePiece_1.png");
    sprite->setPosition(Vec2(size.width/2, size.height/2));
    this->addChild(sprite);
    
    auto delay = DelayTime::create(2);
    //回调函数动作的使用!!!
    sprite->runAction(Sequence::create(delay, CallFuncN::create(CC_CALLBACK_1(GameScene::menuCallback, this)), NULL));
    
    
    return true;
}

void GameScene::menuCallback(Ref* pSender)
{
    Director::getInstance()->replaceScene(HelloWorld::createScene());
}

void GameScene::onEnter()
{
    Layer::onEnter();
    log("GameScene onEnter");
}

void GameScene::onEnterTransitionDidFinish()
{
    Layer::onEnterTransitionDidFinish();
    log("GameScene onEnterTransitionDidFinish");
}

void GameScene::onExit()
{
    Layer::onExit();
    log("GameScene onExit");
}

void GameScene::onExitTransitionDidStart()
{
    Layer::onExitTransitionDidStart();
    log("GameScene onExitTransitionDidStart");
}


你可能感兴趣的:(Cocos2d-x3.2 场景的生命周期)