gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Sprite Frames and Timers

#6694 - einhaender - Mon Jun 02, 2003 1:27 am

Hi, iam new to gba coding and i started playing a bit with tutorial source codes from thepernproject.com. I created me a 32x32 pixel sprite with 4 frames of animation. Now I wanted to switch to the next frame every 2 seconds without any user input. However after waiting an initial first second my animation starts playing rapidly und uncontrolled and I guess it might have something to do with the while loop. Anyone can propose a better solution?
Heres the code snippet iam using in my main():

while(1) {
Wait(1);
switchFrame();
WaitForVsync();
CopyOAM();
}

And my switchFrame() looks like this:
void switchFrame() {
Sprite.activeFrame = Sprite.activeFrame == 3 ? 0 : Sprite.activeFrame += 1;
sprites[0].attribute2 = Sprite.spriteFrame[Sprite.activeFrame];
}

#6696 - DekuTree64 - Mon Jun 02, 2003 1:44 am

Generally you give your sprite a counter that increments every VSync, and when it gets to a certain numbr, you change the sprite's frame, and set the counter back to 0.
So, to change every 2 seconds, set your delay variable to 120 (60 frames per second * 2 seconds), and in your main while loop, put
sprite.delayCnt++;
if(sprite.delayCnt >= sprite.delay)
{
sprite.delayCnt = 0;
switchFrame();
}

The speed thing probably has to do with your Wait(1) or WaitForVSync(). I assume Wait(1) means wait 1 second, but if you use the counter thing, it doesn't need to be there. Just WaitForVSync().

#6723 - einhaender - Mon Jun 02, 2003 8:57 am

Thanks! This worked great. Gave my Sprite struct another attribute called .delayCount, which increments every while-loop. When reaching 120
I call my SwitchFrame() method and I got rid of the Wait(..) function.