Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);

(loop) 
//blit some image 
attack.push(imageInstance); 
(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?

share|improve this question
1  
tl;dr how do I store and play frame animations? –  The Communist Duck May 16 '11 at 12:20
    
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. –  thedaian May 16 '11 at 20:37
    
how would a 2d array be used in this instance? –  Saad May 17 '11 at 5:33

2 Answers 2

up vote 2 down vote accepted

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);
};
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.