Search code examples
c++cocos2d-iphonecocos2d-x

After replacing Scene, the Schedule method doesn't work anymore


I'm making a game in cocos2dx , so I've made a class named CoCoGui and I've also made an IntroPage class that inherits from CCLayerColor for the intro page of the game and a StartPage class that's been inherited from CCLayerColor, too. I want to show the intro page for the first 2 seconds and then show the StartingPage but in the updateGame function of CoCoGui (which is the main loop of the game), when the replaceScene method called, and the Scene become replaced, the updateGame method won't be called anymore! Please help me with this problem thanks!

Here's the CoCoGui.h file: StartingPage and IntroPage are two classes that inherit from CCLayerColor

#ifndef _COCOGUI_H_
#define _COCOGUI_H_

#include "StartingPage.h"
#include "..\Classes\WorkSpace.h"
#include "..\Classes\GameBoard.h"
#include "..\Classes\IntroPage.h"
using namespace cocos2d;

class CoCoGui : public CCLayerColor{

public:
    CoCoGui();
    void addScene (CCScene * startPage, CCScene * work);
    virtual ~CoCoGui(void);
    void updateGame ( float dt );
    virtual bool init();
    static CCScene* scene();
    CREATE_FUNC(CoCoGui);
private:
    bool isInit;
    CCScene * runnigScene;
    IntroPage * introPage;
    StartingPage * startingPage;
    void onEnterTransitionDidFinish();
    void menuCloseCallback(CCObject* pSender);
public:
    CCScene * getRunningScene(void);
};

#endif /* COCOGUI_H */

also here is CoCoGui.cpp file

#include "CoCoGui.h"
#include <iostream>
using namespace std;
CCScene* CoCoGui::scene(){
    CCScene *scene = CCScene::create();
    CoCoGui *layer = CoCoGui::create();

    scene->addChild(layer);

    return scene;
}

CoCoGui::CoCoGui ( )
{
    this->isInit = false;
    this->introPage = new IntroPage ( );
    this->startingPage = new StartingPage ( );
}

CoCoGui::~CoCoGui(void)
{
    delete introPage;
    delete startingPage;
}

void CoCoGui::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

bool CoCoGui::init ( ){
    if ( !CCLayerColor::initWithColor ( ccc4 (100,100,100,255) ) ){
        return false;
}
    this->schedule ( schedule_selector ( CoCoGui::updateGame ), 0.5 );
    return true;
}

void CoCoGui::updateGame ( float dt ){
    cout << "Update Called" << endl;
    if ( !isInit )
        return;
    CCScene * scene = NULL;
    if ( !this->introPage->isIntroPageDone ( ) ){
        scene = IntroPage::scene();
    }
    else if ( this->introPage->isIntroPageDone ( )  ){
        scene = StartingPage::scene();
}
    CCDirector::sharedDirector()->replaceScene(scene);
}

void CoCoGui::onEnterTransitionDidFinish ( ){
    isInit = true;
}


CCScene * CoCoGui::getRunningScene(void)
{
    return this->runnigScene;
}

Solution

  • If this is anything like cocos2d-iphone, you'll have to call the base class implementation of onEnterTransitionDidFinish and similar onEnter/onExit overrides. In cocos2d-iphone not calling super in some of these methods can cause scheduling and input to stop working.