2016-03-16 204 views
1

我正在製作滑塊拼圖,此功能應移動拼貼,然後返回到主要功能以便繼續播放,我也可以使用此功能來隨機播放在遊戲開始的時候,當我稱之爲所有的瓷磚移動到其他地方並進入錯誤的地點或離開屏幕時,這是因爲不是運行moveTo,而是需要0.2秒,然後在0.2秒後返回,它開始動作並立即返回,所以當再次被調用時,瓷磚仍在移動,將所有東西都搞亂。Cocos2d-x C++ - 在繼續之前等待動作完成之前

有沒有什麼辦法阻止代碼在動作完成之前繼續?感謝;)

bool GameScene::updateTile(Sprite* tile, int newPos) 
{ 
    auto moveTo = MoveTo::create(0.2, Vec2(widthArray[newPos], heightArray[newPos])); 
    auto whatever = CallFunc::create([]() { 
     CCLOG("hi"); 
    }); 
    auto seq = Sequence::create(moveTo, whatever, nullptr); 
    tile->runAction(seq); 
    CCLOG("running"); 
    return true; 
} 

//編輯

此代碼打印: 「跑」 和之後0.2秒(時間需要運行動作的moveTo) 「HI」(從動作無論)

我的目標是爲它等待0.2秒,然後打印「喜」和,只有在此之後,打印「跑」,但如果我嘗試使用任何類型的延遲或環 喜歡的停止代碼:

bool GameScene::updateTile(Sprite* tile, int newPos) 
    { 
     auto moveTo = MoveTo::create(0.2, Vec2(widthArray[newPos], heightArray[newPos])); 
     auto whatever = CallFunc::create([]() { 
      CCLOG("hi"); 
     }); 
     auto seq = Sequence::create(moveTo, whatever, nullptr); 
     tile->runAction(seq); 
     while (tile->getNumberOfRunningActions != 0) 
       { //delay 
       } 
     CCLOG("running"); 
     return true; 
    } 

它不運行任何操作,只是卡住了,有什麼想法?

+0

的Cocos2D-X是單線程。 while循環將使其陷入困境。使用預定的函數而不是while循環。看我的edit2。 – Zen

回答

3

您可以使用tag檢查動作是否已完成:

bool GameScene::updateTile(Sprite* tile, int newPos) 
{ 
    static const int MY_MOVE_SEQ = 3333; 
    if (tile->getActionByTag(MY_MOVE_SEQ) && !tile->getActionByTag(MY_MOVE_SEQ)->isDone()) { 
     return; 
     //or you can stop the last action sequence and then run new action 
     //tile->stopActionByTag(MY_MOVE_SEQ); 
    } 

    auto moveTo = MoveTo::create(0.2, Vec2(widthArray[newPos], heightArray[newPos])); 
    auto whatever = CallFunc::create([]() { 
     CCLOG("hi"); 
    }); 
    auto seq = Sequence::create(moveTo, whatever, nullptr); 
    //set tag here! 
    seq->setTag(MY_MOVE_SEQ); 

    tile->runAction(seq); 
    CCLOG("running"); 
    return true; 
} 

//編輯

bool anyActionRunning = false; 
    tile->getParent()->enumerateChildren("//Tile", [&anyActionRunning](Node* child) { 
     if (child->getActionByTag(MY_MOVE_SEQ) && !child->getActionByTag(MY_MOVE_SEQ)->isDone()) { 
      anyActionRunning = true; 
      return true; 
     } 
     return false; 
    }); 

// EDIT2:

static const string funcName = "CheckAction"; 
tile->schedule([tile](float dt) { 
    if (tile->getNumberOfRunningActions == 0) { 
     CCLOG("running"); 
     tile->unschedule(funcName); 
    } 
}, funcName); 
+0

嗨,用這個我可以阻止當前磁貼執行另一個動作,而且他們不會再離開屏幕,但是我想要做的是在執行CCLOG之前先完成所有動作的序列後再等待( 「運行」);並繼續在代碼 – Gualtiero

+0

不真的明白你的問題。但我想你可以使用'enumerateChildren'和定時器做@ Gualtiero – Zen

+0

好吧,我編輯的問題,以使其更清晰,問題是,這個代碼打印: 運行 和後0.2秒(時間需要運行行動的moveTo) 喜(從動作等等) 我的目標是爲它等待0.2秒,然後打印喜,只有在此之後,打印運行,但如果我嘗試停止使用任何類型的延遲或循環它不代碼運行任何行動,只是卡住了,有什麼想法? – Gualtiero

相關問題