这是我的更新方法,当前循环遍历我的 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