C++ cocos2d-X中bool-bRet-do-while循环的意义是什么?

C++ cocos2d-X中bool-bRet-do-while循环的意义是什么?,c++,cocos2d-x,C++,Cocos2d X,我是Cocos2D-X的不速之客,但我已经编程很长时间了。。。我想知道这段代码的意义是什么: 我的困惑主要在于这一部分: 布尔-布雷特=假; 做 {}while(0) 以下是给出一些上下文的整个方法: bool GameScene::init() { CCLog("GameScene::init"); bool bRet = false; do { ///////////////////////////////////////

我是Cocos2D-X的不速之客,但我已经编程很长时间了。。。我想知道这段代码的意义是什么:

我的困惑主要在于这一部分:

布尔-布雷特=假; 做 {}while(0)

以下是给出一些上下文的整个方法:

  bool GameScene::init()
    {
        CCLog("GameScene::init");
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////
        CC_BREAK_IF(! CCLayer::init());

            // Initialize the parent - gets the sprite sheet loaded, sets the background and inits the clouds
            MainScene::init();

            // Start off as game suspended
            gameSuspended = true;

            // Get the bird sprite
            CCSprite *bird = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("bird.png"));
            this->addChild(bird, 4, kBird);

            // Initialize the platforms
            initPlatforms();

            // Create the bonus sprite
            CCSprite *bonus;

            // Load in the bonus images, 5, 10, 50, 100
            for(int i=0; i<kNumBonuses; i++) 
            {
                    bonus = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(bonus_image[i]));
                    this->addChild(bonus,4, kBonusStartTag+i);
                    bonus->setVisible(false);
            }

            // Create the Score Label
            CCLabelBMFont* scoreLabel = CCLabelBMFont::labelWithString("0",  "Images/bitmapFont.fnt");
            this->addChild(scoreLabel, 5, kScoreLabel);

            // Center the label
            scoreLabel->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height - 50));

            // Start the GameScene stepping
            schedule(schedule_selector(GameScene::step));

            // Enable the touch events
            setTouchEnabled(true);
            // Enable accelerometer events
            setAccelerometerEnabled(true);

            // Start the game
            startGame();

    bRet = true;
} while (0);

return bRet;
}
bool游戏场景::init()
{
CCLog(“游戏场景::初始化”);
布尔-布雷特=假;
做
{
//////////////////////////////////////////////////////////////////////////
//超级初始化优先
//////////////////////////////////////////////////////////////////////////
如果(!CCLayer::init());
//初始化父对象-加载精灵工作表,设置背景并初始化云
mainsecene::init();
//比赛暂停后开始
gameSuspended=true;
//去拿鸟精灵
CCSprite*bird=CCSprite::CreateWithPriteFrame(CCSpriteFrameCache::SharedPriteFrameCache()->spriteFrameByName(“bird.png”);
这->addChild(bird,4,kBird);
//初始化平台
初始化平台();
//创建奖励精灵
CCSprite*奖金;
//加载奖励图片,5,10,50,100
对于(inti=0;ispriteFrameByName(bonus_image[i]);
此->添加子项(奖金,4,kBonusStartTag+i);
奖金->设置可见(假);
}
//创建分数标签
CCLabelBMFont*scoreLabel=CCLabelBMFont::labelWithString(“0”,“Images/bitmapFont.fnt”);
此->添加子项(scoreLabel,5,kScoreLabel);
//将标签居中
scoreLabel->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height-50));
//开始游戏场景
时间表(时间表选择器(游戏场景::步骤));
//启用触摸事件
setTouchEnabled(真);
//启用加速计事件
setAccelerometerEnabled(正确);
//开始比赛
startGame();
bRet=真;
}而(0);
返回布雷特;
}
此代码来自:

这是一款开源游戏

我知道bRet代表布尔返回值,但我对一些事情感到困惑。。。我对此感到困惑的一个原因是,为什么这样的程序?其次,如果bRet==false正好等于0,while循环如何知道它的时间。。。我错过什么了吗


我的另一个问题是,您如何知道何时使用CCdataType*varName=…,而不是CCdataType*pVarName=。。。我知道第二个是指针,但也许我遗漏了什么。。。我不明白有什么区别。第一个是尊重声明吗

您的示例忽略了解释一切的关键部分—代码中的真实逻辑。我不是可可豆方面的专家,但据我所见,可可豆通常是这样使用的:

bool bRet = false;
do 
{
    CC_BREAK_IF(!conditionA); // same as  if (!conditionA) break;
    ... some code which possibly sets bRet
    CC_BREAK_IF(!conditionB);        
    ... some other code which possibly sets bRet
    CC_BREAK_IF(!conditionC);        
    ... some other code which possibly sets bRet
    bRet = true;
} while (0);
return bRet;
在这种情况下,它允许代码跳转到return语句,而不需要求助于
goto
,或者嵌套一堆
if
语句。将其与此进行比较:

bool bRet = false;
if (conditionA); 
{
    ... some code which possibly sets bRet
    if (conditionB)
    {
        ... some other code which possibly sets bRet
        if (conditionC);        
        {
            ... some other code which possibly sets bRet
        }
    }
}
bRet = true;

return bRet;

您的示例忽略了解释一切的基本部分—代码中的真实逻辑。我不是可可豆方面的专家,但据我所见,可可豆通常是这样使用的:

bool bRet = false;
do 
{
    CC_BREAK_IF(!conditionA); // same as  if (!conditionA) break;
    ... some code which possibly sets bRet
    CC_BREAK_IF(!conditionB);        
    ... some other code which possibly sets bRet
    CC_BREAK_IF(!conditionC);        
    ... some other code which possibly sets bRet
    bRet = true;
} while (0);
return bRet;
在这种情况下,它允许代码跳转到return语句,而不需要求助于
goto
,或者嵌套一堆
if
语句。将其与此进行比较:

bool bRet = false;
if (conditionA); 
{
    ... some code which possibly sets bRet
    if (conditionB)
    {
        ... some other code which possibly sets bRet
        if (conditionC);        
        {
            ... some other code which possibly sets bRet
        }
    }
}
bRet = true;

return bRet;
我找到了

在此之前,Cocos2d-x的开发人员编写了代码,通过
goto
s:

#define check(ret)  if(!ret) goto cleanup;

void func()
{
  bool bRet = false;

  bRet = doSomething();
  check(bRet);
  bRet = doSomethingElse();
  check(bRet);

  bRet = true;

cleanup:
  // Do clean up here

  return bRet;
}
如您所见,如果在过程中出现任何问题,这是一种非常讨厌的方法,可以在函数末尾跳到清理。对函数的每次调用都返回函数是否成功。然后使用
check
宏查看
bRet
是否为真,如果为真,则直接跳到
cleanup
标签

然后他决定去掉
goto
s,并将其从
do while
循环更改为
break

#define CC_BREAK_IF(cond)  if(!cond) break;

void func()
{
  bool bRet = false;

  do {
    bRet = doSomething();
    CC_BREAK_IF(bRet);
    bRet = doSomethingElse();
    CC_BREAK_IF(bRet);

    bRet = true;
  } while (0);

  // Do clean up here

  return bRet;
}
这正是同样的效果。它只是使用
break
作为
goto
机制,在
do while
循环之后跳转到代码。

我发现

在此之前,Cocos2d-x的开发人员编写了代码,通过
goto
s:

#define check(ret)  if(!ret) goto cleanup;

void func()
{
  bool bRet = false;

  bRet = doSomething();
  check(bRet);
  bRet = doSomethingElse();
  check(bRet);

  bRet = true;

cleanup:
  // Do clean up here

  return bRet;
}
如您所见,如果在过程中出现任何问题,这是一种非常讨厌的方法,可以在函数末尾跳到清理。对函数的每次调用都返回函数是否成功。然后使用
check
宏查看
bRet
是否为真,如果为真,则直接跳到
cleanup
标签

然后他决定去掉
goto
s,并将其从
do while
循环更改为
break

#define CC_BREAK_IF(cond)  if(!cond) break;

void func()
{
  bool bRet = false;

  do {
    bRet = doSomething();
    CC_BREAK_IF(bRet);
    bRet = doSomethingElse();
    CC_BREAK_IF(bRet);

    bRet = true;
  } while (0);

  // Do clean up here

  return bRet;
}

这正是同样的效果。它只是使用
break
作为
goto
机制,在
do while
循环之后跳转到代码。

没有意义,这只是一种糟糕的风格。整个方法可以(也应该)改写为:

bool GameScene::init()
{
    CCLog("GameScene::init");

    if (!CCLayer::init())
        return false;

    // Initialize the parent - gets the sprite sheet loaded, sets the background and inits the clouds
    MainScene::init();

    // … lots of scene setup code here ...

    return true;
}
我见过引擎运行其主循环的类似代码,但在这种情况下,它将是一个无止境的循环:

do
{
    // run game loop as fast as it can

    // end the game
    if (userQuits)
        break;
} while(true);
即使您需要额外的作用域,例如为了避免局部变量的名称冲突,一对额外的大括号就足够了。像这样:

{
    int x = 10;
    // do stuff
}
{
    int x = 234; // this x is in its own scope
    // do other stuff
}

没有意义,这只是一种糟糕的风格。整个方法可以(也应该)改写为:

bool GameScene::init()
{
    CCLog("GameScene::init");

    if (!CCLayer::init())
        return false;

    // Initialize the parent - gets the sprite sheet loaded, sets the background and inits the clouds
    MainScene::init();

    // … lots of scene setup code here ...

    return true;
}
我见过引擎运行其主循环的类似代码,但在这种情况下,它将是一个无止境的循环:

do
{
    // run game loop as fast as it can

    // end the game
    if (userQuits)
        break;
} while(true);
即使您需要额外的作用域,例如为了避免局部变量的名称冲突,一对额外的大括号就足够了。像这样:

{
    int x = 10;
    // do stuff
}
{
    int x = 234; // this x is in its own scope
    // do other stuff
}

这就是全部密码<当(0)是一个nop时,代码>做{}似乎缺少了一些东西