0

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

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 );
}
4

1 回答 1

0

终于我找到了答案!我们应该使用静态变量,这样它们就不会在主循环后被破坏

于 2013-02-09T16:19:37.147 回答