0

这是我的更新方法,当前循环遍历我的 6 个行走角色的精灵,它计数 0、1、2、3、4、5,然后重置回 0。

任务是让它向前循环然后向后循环0,1,2,3,4,5,4,3,2,1,0,1,2...等

我已经尝试实现几种计数方法来在特定条件下进行计数,但它们似乎在第 4/5 帧之间打架和循环。

有没有快速的解决方案?或者任何人都可以向我指出解决方案的方向:)

    void SpriteGame::Update(int tickTotal, int tickDelta){

    if ( tickTotal >= this->playerLastFrameChange + (TICKS_PER_SECOND / playerSheetLength) )
    {
        this->playerFrame = this->playerFrame + 1;
        this->playerLastFrameChange = tickTotal;

        if (this->playerFrame >= this->playerSheetLength)
        {
            this->playerFrame = 0;
        }

        this->playerSourceRect->left = this->playerFrame * widthOfSprite;
        this->playerSourceRect->top = 0;
        this->playerSourceRect->right = (this->playerFrame + 1) * widthOfSprite;
        this->playerSourceRect->bottom = widthOfSprite;
    }
}

实施 (abs()) 方法工作计数 0,1,2,3,4,5,4,3,2,1,2.. 等

//initializing playerFrame = -4; at the top of the .cpp

this->playerFrame = this->playerFrame +1; //keep counting for as long as its <= 5 [sheet length]

if (this->playerFrame >= this->playerSheetLength)
{
this->playerFrame = -4;
}

this->playerSourceRect->left = (abs(playerFrame)) * widthOfSprite;
this->playerSourceRect->top = 0;
this->playerSourceRect->right = (abs(playerFrame)+1) *     widthOfSprite;
this->playerSourceRect->bottom = widthOfSprite
4

3 回答 3

0

让它连续向上和向下计数的一种相当简单的方法是将计数从 -4 运行到 5(始终递增)。当它超过 5 时,将其设置回 -4。

要从该计数中获取实际的精灵索引,只需获取它的绝对值 ( abs())。这将为您提供任何负值的正等效值,但保持正值不变。

于 2013-10-24T17:14:32.693 回答
0

像这样的东西怎么样:

char direction=1; //initialize forward

this->playerFrame+=direction;
if(this->playerFrame >= this->playerSheetLength || this->playerFrame <=0)
  direction*=-1;
于 2013-10-24T17:04:32.257 回答
0

要坚持您开始使用的算术方法,您需要类似这样的伪代码

if( /*it's time to update frame*/ )
{
    if( currentFrame >= maxFrame || 
        currentFrame <= minFrame   )
    {
        incrementer *= -1;
    }

    currentFrame += incrementer;
    // Then calculate the next frame boundary based on the frame number

 }

或者,如果您在向量中保留一组矩形,您可以简单地迭代到结束,然后反向迭代到开始,等等。

谷歌std::vector, std::vector::begin(), std::vector::end(), std::vector::rbegin(), std::vector::rend()

快速示例:

vector<rect> vec;
.
.
.
for(vector<rect>::iterator iter = vec.begin(); iter != vec.end(); ++iter)
{
    ....
}

for(vector<rect>::reverse_iterator iter = vec.rbegin(); iter != vec.rend(); ++iter)
{
    ....
}
于 2013-10-24T17:05:39.903 回答