ゲーム開発日記

楽しいよね。

Scene遷移の順番について(onEnter,onExit, onEnterTransitionDidFinish)

FirstSceneから初めて、クリックしたらSecondSceneにかわり、またクリックするとSecondSceneにかわる時のメソッドの順番を調べました。

FirstScene.h

#include "cocos2d.h"


class FirstScene : public cocos2d::CCLayer
{
public:
    FirstScene();
    ~FirstScene();
    
    static cocos2d::CCScene* scene();
    
    virtual bool init();
    
    virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
    
    virtual void onExit();
    virtual void onEnter();
    virtual void onEnterTransitionDidFinish();
    
    CREATE_FUNC(FirstScene);
};

FirstScene.cpp

#include "FirstScene.h"
#include "SecondScene.h"

using namespace cocos2d;

CCScene* FirstScene::scene()
{
	CCLOG("===========================================");
	CCLOG("%s: FirstScene", __FUNCTION__);
    
    CCScene* scene = CCScene::create();
    FirstScene* layer = FirstScene::create();
    
    scene->addChild(layer);
    return scene;
}

bool FirstScene::init()
{
    if (CCLayer::init())
    {
        CCLOG("%s: FirstScene", __FUNCTION__);
        
        CCLabelTTF* label = CCLabelTTF::create("First Scene", "Marker Felt", 64);
        label->setColor(ccGREEN);
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        label->setPosition(CCPointMake(size.width / 2, size.height / 2));
        this->addChild(label);
        
        this->setTouchEnabled(true);
        
        return true;
    }
    return false;
}

FirstScene::FirstScene()
{
    CCLOG("%s: FirstScene", __FUNCTION__);
}

FirstScene::~FirstScene()
{
    CCLOG("%s: FirstScene", __FUNCTION__);
}

bool FirstScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    CCLOG("%s:FirstScene", __FUNCTION__);
    CCDirector::sharedDirector()->replaceScene(SecondScene::scene());
    
    return true;
}

void FirstScene::onExit()
{
    CCLOG("%s:FirstScene", __FUNCTION__);
    
    CCLayer::onExit();
}

void FirstScene::onEnter()
{
    CCLOG("%s: FirstScene", __FUNCTION__);
    
    CCLayer::onEnter();
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}

void FirstScene::onEnterTransitionDidFinish()
{
    CCLOG("%s: FirstScene", __FUNCTION__);
    
    CCLayer::onEnterTransitionDidFinish();
}

SecondScene.h

#include "cocos2d.h"


class SecondScene : public cocos2d::CCLayer
{
public:
    SecondScene();
    ~SecondScene();
    
    static cocos2d::CCScene* scene();
    
    bool init();
    
    virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
    
    virtual void onExit();
    virtual void onEnter();
    virtual void onEnterTransitionDidFinish();
    
    CREATE_FUNC(SecondScene);
};
#include "FirstScene.h"
#include "SecondScene.h"


using namespace cocos2d;

CCScene* SecondScene::scene()
{
	CCLOG("===========================================");
	CCLOG("%s: SecondScene", __FUNCTION__);
    
    CCScene* scene = CCScene::create();
    CCLayer* layer = SecondScene::create();
    
    scene->addChild(layer);
    return scene;
}

bool SecondScene::init()
{
    if (CCLayer::init())
    {
        CCLOG("%s: SecondScene", __FUNCTION__);
        
        CCLabelTTF* label = CCLabelTTF::create("Second Scene", "Marker Felt", 64);
        label->setColor(ccGREEN);
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        label->setPosition(CCPointMake(size.width / 2, size.height / 2));
        this->addChild(label);
        
        this->setTouchEnabled(true);
        
        return true;
    }
    return false;
}

SecondScene::SecondScene()
{
    CCLOG("%s: SecondScene", __FUNCTION__);
}


SecondScene::~SecondScene()
{
    CCLOG("%s: SecondScene", __FUNCTION__);
}

bool SecondScene::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    CCDirector::sharedDirector()->replaceScene(FirstScene::scene());
    
    return true;
}

void SecondScene::onExit()
{
    CCLOG("%s: SecondScene", __FUNCTION__);
    
    CCLayer::onExit();
}

void SecondScene::onEnter()
{
    CCLOG("%s: SecondScene", __FUNCTION__);
    
    CCLayer::onEnter();
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}

void SecondScene::onEnterTransitionDidFinish()
{
    CCLOG("%s: SecondScene", __FUNCTION__);
    
    CCLayer::onEnterTransitionDidFinish();
}


ログ:
Cocos2d: ===========================================
Cocos2d: scene: FirstScene
Cocos2d: FirstScene: FirstScene
Cocos2d: init: FirstScene
Cocos2d: onEnter: FirstScene
Cocos2d: onEnterTransitionDidFinish: FirstScene
Cocos2d: ccTouchBegan:FirstScene
Cocos2d: ===========================================
Cocos2d: scene: SecondScene
Cocos2d: SecondScene: SecondScene
Cocos2d: init: SecondScene
Cocos2d: onExit:FirstScene
Cocos2d: ~FirstScene: FirstScene
Cocos2d: onEnter: SecondScene
Cocos2d: onEnterTransitionDidFinish: SecondScene


SecondScene::initの後にFirstScene::onExitとデストラクタが呼び出されており、
シーンの狭間でFirstSceneとSecondSceneの両方がメモリにのっかてしまう問題って解消したんだろうか?時間あればもう少し実験する。

inspired by

cocos2dで作る iPhone&iPadゲームプログラミング

cocos2dで作る iPhone&iPadゲームプログラミング

第5章-1