So I'm thinking about making a small rpg, mainly to test out different design patterns I've been learning about. But the one question that I'm not too sure on how to approach is how to store an array of character moves in the best way possible.

So let's say I have arrays of different sprites.

This is how I'm thinking about implementing it:

array attack = new array (10); 
array attack2 = new array(5);

//blit some image 
(end loop)

Now every time I want the animation I call on attack or attack2; is there a better structure? The problem with this is let's say there are 100 different attacks, and a player can have up to 10 attacks equipped. So how do I tell which attack the user has; should I use a hash map?

  • 1
    \$\begingroup\$ tl;dr how do I store and play frame animations? \$\endgroup\$ May 16, 2011 at 12:20
  • \$\begingroup\$ Not enough for a full answer, but: You could use a hash map, a 2d array, a state machine, or something else someone here will point out. \$\endgroup\$
    – thedaian
    May 16, 2011 at 20:37
  • \$\begingroup\$ how would a 2d array be used in this instance? \$\endgroup\$
    – Saad
    May 17, 2011 at 5:33

2 Answers 2


There are many ways to implement animations, generally you would use a sprite sheet. This can get compilcated, but an easy way is to predefine character size, for example 128*64, and pre-calculate the number of lines and columns to get the total number of frames. I assume you are familiar with this however.

You could end up with something like this (C# esque assuming tightly packed sprite sheet) :

class Animation {
    Texture2D spriteSheet;
    ushort rows, columns;

    Animation(ushort spriteWidth, ushort spriteHeight)
        rows = spriteSheet.Height / spriteHeight;
        columns = spriteSheet.Width / spriteWidth;

    Rectangle GetFrame(ushort frameNumber);

If you have hundreds of animations, this will mean hundreds of sprite sheets and lots of memory. If you look at some older games, you could have the same animation for each melee attack, but the weapon's animation was put on top of that. This means a big save in sprite sheets, and could be useful.

To check what attack a player currently has, I would simply have an array of animations (pre-loaded or not) which will be referenced by the player depending on the attacks he chooses. If you choose to decouple parts of the animation, an AttackAnimation could contain multiple animations for the top half of the body, the bottom hals, the weapon, etc.

That could be something like this :

class AttackingAnimation {
    List<Animations> animationList;
    // Draw and update all animations...

class Player {
    AttackingAnimation attackingAnimations[10];
    DoAttack(int attackIndex);

You're gonna want to think at a higher level. Playing and switching among a sequence of frames to create an animations should be encapsulated on the lowest level. It should know nothing about how many, or what attacks the player has.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .