Hello world.
I have a question: how does one time and animate a sprite?
Do I use a timer class and check if a certain amount of milliseconds have passed?
Or do I keep a counter(an integer) that increments each iteration until a certain point is reached?
I know how to clip the sprites, I just need to time the clipping correctly.
Any help and ideas will be appreciated
Sprite animation/timing methods
Moderator: PC Supremacists
- 0x0000000
- Chaos Rift Newbie
- Posts: 42
- Joined: Mon Feb 11, 2013 11:46 pm
- Current Project: Pokemans
- Favorite Gaming Platforms: PC, Gameboy Advance SP, Mac, Xbox360
- Programming Language of Choice: C/++
- Location: Memory Address 0x0000000 (in UTF-9 notation)
- Contact:
Sprite animation/timing methods
Don't ask me about my username.
> viewtopic.php?f=4&t=8520&start=999999#p85581
> viewtopic.php?f=4&t=8520&start=999999#p85581
Re: Sprite animation/timing methods
I'd prefer the first method. Libraries like SFML have classes for this but I'd probably just use a float variable and increment it by the frametime in every update cycle.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Sprite animation/timing methods
I calculate a time delta since the last animation frame change and check if it exceeds the desired delta each update. If it does, go to the next animation frame and reset the deltaTime to 0 subtract the expected duration from the actual duration to minimize error (as per K-Bal's suggestion). This results in a smaller margin of error than basing it solely on frame count without the asynchronicity that a timer introduces.
Psuedocode in case wording was unclear:
Psuedocode in case wording was unclear:
struct Frame { int duration; //... } class Sprite { int frameCount; Frame *frames; int frame; int ticks; void Update(); //... }; void Sprite::Update(int deltaTime) { ticks += deltaTime; if(ticks >= frames[frame].duration) //milliseconds { ticks -= frames[frame].duration; //assumes 1-directional looping order (e.g. 1, 2, 3, 1, 2, 3) //in my actual code this is wrapped in a switch(frameOrder) where enum FrameOrder { None, Loop, Oscillation, ReverseLoop } frame = ++frame % frameCount; } }
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Re: Sprite animation/timing methods
I wouldn't set it to 0 but subtract the keyframe time. This way you're introducing less error.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Sprite animation/timing methods
Good idea, hadn't thought of that. Edited my pseudocode, can you double-check the logic?K-Bal wrote:I wouldn't set it to 0 but subtract the keyframe time. This way you're introducing less error.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!