【问题标题】:member variables of a class has been changed, in each loop ( cocos2dx )在每个循环( cocos2dx )中,类的成员变量已更改
【发布时间】:2012-12-26 14:57:19
【问题描述】:

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

【问题讨论】:

  • 程序每次循环后(CCDirector的主循环)
  • 您的CoCoGui 类具有用户定义的构造函数和析构函数,但没有复制构造函数或赋值运算符。如果有人要复制或分配这样的对象,则包含的指针可能会全部混乱。见The rule of three
  • CREATE_FUNC 宏使用了另一个构造函数,该构造函数没有任何输入类型,如果我将 CREATE_FUNC 更改为类的创建函数,则会返回错误

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多