2016-12-16 78 views
0

以下方法在用戶操作後調用。代碼的目的是在不改變剩餘時間的情況下更改進度計時器的精靈。我的代碼改變了精靈,但它也增加了定時器的剩餘時間。金額每次都有所不同,但總是增加。我究竟做錯了什麼?在cocos2d-x 3.0中,如何在保持百分比的同時更改進度計時器的精靈?

用戶操作之前:

Before User Action

後用戶操作:

After User Action

void TouchScene::switchColors(CustomTimer* centerTimer) 
{ 
    float time = myTimer->getPercentage(); 
    myTimer->runAction(RemoveSelf::create()); 
    createCenterTimer(time); 
} 

void TouchScene::createCustomTimer(float percentage) 
{ 
    myTimer = CustomTimer::create(); 
    int iRandomColor = getNextColor(); 
    std::string colorName = getColorName(iRandomColor); 
    myTimer->setName(colorName); 
    myTimer->setAngle((float)M_PI * 2); 
    /*pass the current percentage to the new timer*/ 
    myTimer->drawTimer(percentage); 
    addChild(myTimer, 0); 
} 

void CustomTimer::drawTimer(float time) 
{ 
    /*removed standard positioning code*/ 

    /*get the name of the next sprite*/ 
    std::string name = getName().c_str(); 
    Sprite *timerSprite = Sprite::create(name + ".png"); 
    timer = ProgressTimer::create(timerSprite); 
    timer->setType(ProgressTimer::Type::RADIAL); 
    timer->setReverseDirection(true); 

    /*set the timer to pick up where the previous one left off...*/ 
    timer->setPercentage(time); //gives inconsistent results 
    addChild(timer); 
} 
+0

您可以顯示CustomTimer的代碼嗎? – bunty

回答

0

無需去除ProgressTimer的當前實例,只是不同的重置新的精靈在CustomTimer對象上使用setSprite顏色將工作f國家統計局。以下是示例代碼:

void TestGame::drawTimer(float time) { 
    Sprite *timerSprite = Sprite::create("blueCircle.png"); 
    myTimer = ProgressTimer::create(timerSprite); 
    myTimer->setPosition(Vec2(100,100)); 
    myTimer->setType(ProgressTimer::Type::RADIAL); 
    myTimer->setReverseDirection(true); 
    myTimer->setPercentage(100); 
    this->addChild(myTimer); 

    myTimer->runAction(ProgressFromTo::create(10, time, 0)); 

    auto cb = CallFunc::create(CC_CALLBACK_0(TestGame::changeColor, this)); 
    this->runAction(Sequence::create(DelayTime::create(5), cb, NULL)); 

} 
void TestGame::changeColor() { 
    Sprite *timerSprite = Sprite::create("redCircle.png"); 
    myTimer->setSprite(timerSprite); 
} 
+0

@cpax,請讓我知道它是否有效。 – bunty