I 've declared some classes in my cocos2dx program, and I 've set some values for their member variables, but after each time the program loops ( the main loop of the CCDirector ), all of their values have been removed! I want it class to save its next scene that should be replaced or pushed when the scene should be replaced (as you can see in the code, after 2 second, the introPage class calls a function "IntoPageDone" and the scene should be replaced )and I 've set a variable named nextScene for the next Scene, but after each loop, its value changes into NULL. plz help me with this problem, and also is there some better ways to handling the scenes and changes of them?? tnx a lot!
StartingPage is a class which is inherited from CCLayerColor. Here is CoCoGui.h file:
#ifndef _COCOGUI_H_
#define _COCOGUI_H_
#include "StartingPage.h"
#include "IntroPage.h"
using namespace cocos2d;
class CoCoGui : public CCLayerColor{
public:
CoCoGui();
virtual ~CoCoGui(void);
virtual bool init();
static CCScene* scene();
CREATE_FUNC(CoCoGui);
private:
IntroPage * introPage;
StartingPage * startingPage;
void onEnterTransitionDidFinish();
};
#endif /* COCOGUI_H */
And this 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->startingPage = new StartingPage ( );
this->introPage = new IntroPage ( );
}
CoCoGui::~CoCoGui(void)
{
delete introPage;
delete startingPage;
}
bool CoCoGui::init ( ){
if ( !CCLayerColor::initWithColor ( ccc4 (100,100,100,255) ) ){
return false;
}
return true;
}
void CoCoGui::onEnterTransitionDidFinish ( ){
this->introPage->setNextScene ( StartingPage::scene( ) );
CCScene * scene = NULL;
scene = IntroPage::scene();
CCTransitionFade * trans = CCTransitionFade::create( 0.4f, scene , ccBLACK);
CCDirector::sharedDirector()->pushScene(trans);
cout << "step" << endl;
}
This is introPage.h file:
#ifndef INTROPAGE_H_
#define INTROPAGE_H_
#include "cocos2d.h"
#include "StartingPage.h"
using namespace cocos2d;
class IntroPage: public CCLayerColor {
public:
IntroPage( CCScene * nextScene );
IntroPage ( );
virtual ~IntroPage();
static CCScene* scene();
bool init();
void intoPageDone();
CREATE_FUNC(IntroPage);
CC_SYNTHESIZE_READONLY(CCLabelTTF*, _label, Label);
CCScene * getNextScene( );
void setNextScene ( CCScene * nScene );
private:
CCScene * nextScene;
};
#endif /* INTROPAGE_H_ */
And also IntroPage.cpp:
#include "IntroPage.h"
IntroPage::IntroPage( CCScene * nextScene ) {
this->introPageDone = false;
this->setNextScene ( nextScene );
}
IntroPage::IntroPage( ){
}
IntroPage::~IntroPage() {
}
void IntroPage::setNextScene ( CCScene * nScene ){
this->nextScene = nScene;
}
CCScene* IntroPage::scene(){
CCScene *scene = CCScene::create();
IntroPage *layer = IntroPage::create();
scene->addChild(layer);
return scene;
}
bool IntroPage::init() {
if (CCLayerColor::initWithColor (ccc4(0, 0, 0, 255) )) {
this->runAction(
CCSequence::actions(CCDelayTime::actionWithDuration(2),
CCCallFunc::actionWithTarget(this,
callfunc_selector(IntroPage::intoPageDone)),
NULL));
return true;
}
return false;
}
void IntroPage::intoPageDone() {
this->introPageDone = true;
CCDirector::sharedDirector( )->popScene( );
CCDirector::sharedDirector( )->pushScene( this->nextScene );
}
Finally I found the answer! We should use static variable, so they won't destructed after main loop