0

所以我试图在 C++ 中使用一个全局布尔变量,但它总是返回 false。我在 mainmenuscene.h 文件中定义了值:

#ifndef MAINMENUSCENE_H_
#define MAINMENUSCENE_H_
#include "cocos2d.h"

extern bool boo_startgame;


class MainMenu : public cocos2d::CCLayer
{....}

在我的 mainmenuscene.cpp 文件中,我将其设置为 true:

void MainMenu::menuStartSinglePlayer(CCObject* pSender)
{
    boo_startgame=true;
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();

    // run
    pDirector->pushScene(pScene);
}

但是在我的 helloworldscene.cpp 文件中,它被设置为 false。

#include "HelloWorldScene.h"
bool boo_startgame;

bool HelloWorld::init()
        {
               CCLog(boo_startgame ? "boo_startgame=true" : "boo_startgame=false");
               if (boo_startgame==true)
                   {
                     int_dealer=(arc4random() % 4);
                   }
    ....}

无论出于何种原因,if 语句中的部分永远不会触发,所以我知道这不是我的 CCLog 代码搞砸了。这里发生了什么?

4

1 回答 1

-1

您可以尝试声明您的布尔值,就volatile bool boo_startgame好像它可以在一个执行线程中修改并在另一个执行线程中测试一样。

于 2013-07-06T20:53:35.600 回答