Sign in to follow this  

2D-sprite-animation control-structures?!

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, here I come with another problem. I'm currently working on a 2D-sprite-engine using Direct3D. I built a T2DXAnimatedSprite- and T2DXAnimatedSpriteEx-class. As the name says, they're able to animate a given Texture, by showing different Frames. The AnimatedSpriteEx-class is able to have different framesets, that define which frames of the texture to use. For example 'walking' uses frames 0 to 10, 'jumping' uses 11 to 20 and so on. But now, I need something for a better control of the animations. For example, I'm building a little space-ship-game to test the engine. The frames of the ship's texture show a rotation. Now I want the ship to rotate a little (just 3or4 frames) to the left if it's going to the left and the other way around, like it's leaning in the direction. If the moving stops, it shall lean back in the normal position. Now, just to control that the animations are connected properly (for example, that the ship leans back before leaning to the opposite site) I already need a lot of code (42lines for both movements). If I now would want to implement a complete rotation by pressing the space button, I would have to add a whole lot of additional if-clauses. I'm using Delphi and now am searching for a possibility to control animation in a simple way. I don't need code examples, I just need the theory behind it :) Please tell me ways to control animations, cause I'm a little new to that :P I hope you're getting my problem. .ch!cken

Share this post


Link to post
Share on other sites
It sounds like you need a way of finding out if an animation is done, before moving to the next one. You could add a function to your sprite like isAnimationDone() that returns a boolean.

Then ship logic

if left key is pressed
set sprite to turning left animation

if turning left animation is done
set sprite to moving left animation


Also you should look into Finite State Machines or FSM they make life so much easier.

Share this post


Link to post
Share on other sites
I can already find out if the animation is done by comparing the animationstop-frame and the current frame, so this is not really my problem.

But I think the FiniteStateMachines u mentioned might be what I am looking for. I read through the wikipedia article. So, do I write a class for that or what?
Is this what's used for level-control, too?

What I think of now, is that I write a class which contains different states and conditions for entering those states.
So for example:
State = Moving
If button X is pressed then State = Turning
If button Y is pressed then State = Rotating
State = Truning
If button X is pressed then State = Rotating
If button Y is pressed then State = Moving

Something like that? So if I write a class like that, should I implement that class into my sprites, or is that something I should keep out there and just implement into my games?

Thanks

Share this post


Link to post
Share on other sites
Ya it looks like you have the idea. I use FSM for all kinds of things, AI especially

There are a few ways of doing it. Making a FSM class might be overkill for what you're doing though. I find my FSMs are too small and specialized to bother with classes so I just use a c switch statement (I don't know the Delphi equivalent)

Either way putting it into your Sprite class probably isn't the best place for it. Do you have some kind of entity or ship class the uses the sprite class? thats where I would put game specific state stuff. The entity class contains a sprite class and an animationFSM, the animationFSM determines which animation the sprite should be using, and the sprite keeps track of what frame it's on based on it's current animation.

Share this post


Link to post
Share on other sites
I think c switch statement is the same like a case statement in Delphi, which could be compared to a collection of if-clauses, right?

So instead of writing a whole class, I could just use a property with a setter-method...ok, I'll try to implement that. Still seems a little bit tricky and complicated, but looks like there is no other way :-\ :( ;-)

Thank you :)

Share this post


Link to post
Share on other sites

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this