6
\$\begingroup\$

Let's say I'm creating a game (in Java or C++). In this game I have a character that can do a sword swing animation, but the character can equip different swords.

Do I have to make an animation for each individual sword, or is there another way? I Like if a character can equip different armors, do I create a new sprite for each armor, or is there a way to have, say a base character that can perform all the animations with different swords and can have different armors on the same image?

\$\endgroup\$
2

1 Answer 1

8
\$\begingroup\$

Yes, you can do it in a fairly simple way using transparency.

What you'd need to do would be to make a few layers of sprites instead of one, prepared in such a way and order that when they were displayed, they would form a whole person.

I've attached a sample image showing how that might look.

example

  1. Shows the base image I was working on
  2. I've removed parts that are not going to change
  3. I've made one isolated sword image
  4. That's how sword (gold one this time) looks when drawn over the image from 2. Smooth!

Now you can easily replace steel sword with gold sword, and you don't have to create a whole new set of sprites.

If you feel more like an artist at this point

Then simply draw a few (5-10) sword positions (precomputed). This has an additional benefit of allowing you to change the sword shape during movement and might produce overall better results.

The number of animation frames is of course up to you. What's important in this method is that with a proper sprite delay engine (not that hard to write), you can have more frames for parts that move faster and less for ones that move slower.

Simple pseudocode implementation might look like

// calculating each subpart
foreach(sprite_part in parts) 
    // first_not_less_than is usually called "lower_bound"
    sprite_num = time_data[sprite_part_num].first_not_less_than(time)
    sprite = spritesheet[sprite_part[sprite_num]];

If you feel more like a programmer

Notice how I've removed the whole arm from the base. You could easily rotate the sword to simulate the movement. This will produce a very smooth animation and requires only sprite drawn in basic position, but can feel a bit less natural.

Forgive me for not preparing a .gif comparing the two, but I am not an artist :)

While this is only an example, there's of course more to it. From a top-view game or isometric game different approach can make sense. This answer only aims to illustrate the general idea.

\$\endgroup\$

You must log in to answer this question.