Sign in to follow this  
wease

What classes should I build for 2D game animations? Any suggestions?

Recommended Posts

wease    148
I want to make a 2D game and know the basics of using directX. However, I have some questions about the class structure I should use. I was thinking of creating the following: -a class 'Animation' which loads the .tga file for an animation into a texture, records position, size, and handles drawing the animation. -a class which contains something like an array of type 'Animation' for a character (ie, left, right, jump, etc). -collisions would be handled by both classes (if a character were to interact with a single animation for example) I'm sure someone has thought about this problem before and maybe there is a better way to handle animations for 2d games.

Share this post


Link to post
Share on other sites
Mattman    336
The idea of an animation class sounds like it could work. However, I don't think you'd want the animation class itself to handle the collision detection. You'd probably want some sort of "Object" object that represents an object in your game world (whether it represents a magician or a fountain, I don't know). But then I would imagine the "Object" object -- or maybe a child object of it even -- handles the collision detection. The same object would also contain a child (or several child) objects of the Animation class.

Then again I've never released a game. :-P

Share this post


Link to post
Share on other sites
nilkn    960
An animation really is a collection of frames. Therefore, it is misleading to name your image container an "animation". Rather, have the image class named something like "Image", and have a separate animation class which stores a list of these image classes.

For the most flexibility, though, I recommend you abstract even an animation frame. So perhaps have another class (like AnimationFrame) which stores an image pointer and the length in ms the frame should be displayed. Then give your animation class a list of these AnimationFrame classes. This will let you give each frame a unique length to show it.

You would need to give the animation class a timer, which times how long the current frame has been displayed. If the timer is greater than the frame's delay time, increment the current frame number and reset the timer.

<edit>
It really isn't a good idea to place the collision detection code in the animation class. I'd recommend pulling that up to a higher level on the class hierarchy. For example, if you have a base interface for game entities, give it a pure virtual function for collision detection. Then each game entity type can decide how it detects collisions. There may be a more efficient way for certain types rather than pixel-per-pixel checks.
</edit>

HTH,
nilkn

Share this post


Link to post
Share on other sites
wease    148
Ok, thanks for the advice on the collision detection. I think you are right. It is a good idea to move it away from the animations. I will create an object that will handle collisions and contain my animations, etc.

Part of the issue with my code structure is that I intend to have a character that do many animations (ie, walk left, walk right, jump, dance, math homework, etc). I was thinking about having an object for each of these animations and then including them all in an array of something in this character object. However, I recently was told that it is bad to load a bunch of different textures in directX (which I would have to do for each animation). Instead, I'm thinking about including all these frames in one or two textures and having that available in the character class. Is this how 'real' games do it or do they follow some other structure?

Share this post


Link to post
Share on other sites
nilkn    960
It's generally quite a bit more efficient to keep all the frames for an animation in a single image file. The frames are commonly arranged in a grid. Of course, your animation class would become considerably more complex to be able to read and render the individual frames, but it does give considerable performance boosts.

I personally have never had a problem keeping frames in individual image files. I've never had any speed issues, so unless you just want to, it's unnecessary to implement such a system for most games.

Share this post


Link to post
Share on other sites

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