在每个循环中更改了类的成员变量(cocos2dx)

时间:2012-12-26 14:57:19

标签: c++ cocos2d-iphone cocos2d-x game-development

我已经在cocos2dx程序中声明了一些类,并且我为它们的成员变量设置了一些值,但是每次程序循环(CCDirector的主循环)之后,它们的所有值都被删除了! 我希望它类保存下一个场景,应该替换或推动应该替换场景(如你所见,在2秒后,introPage类调用一个函数“IntoPageDone”,场景应该被替换)我为下一个场景设置了一个名为 nextScene 的变量,但在每个循环之后,它的值变为 NULL 。 PLZ帮助我解决这个问题,还有一些更好的方法来处理它们的场景和变化吗? tnx很多!

StartingPage是一个继承自CCLayerColor的类。 这是 CoCoGui.h 文件:

#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 */

这是 CoCoGui.cpp 文件:

#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;
}

这是 introPage.h 文件:

#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_ */

还有 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 );
}

1 个答案:

答案 0 :(得分:0)

最后我找到了答案! 我们应该使用静态变量,因此它们不会在主循环

之后被破坏