views:

537

answers:

2

Hello, I'm working on a game where my character needs to be able to have different weapons. For that I think that somehow overlaying the weapon over the moving sprite would be the correct choice, but I'm not sure about how could I do this.

Assuming my Character spritesheet looks like this:

Player SpriteSheet

And my preliminar weapon spritesheet ( haven't decided on a fixed square size for the weapon yet ), looks like this:

Weapon SpriteSheet

How would you make the overlay to set the weapon correctly over the character hand for each of his frames? I know that one way would be just to have a weapon frame the same size as my character sprites, and overlay those too, but I think that if the game has way too much weapons (say 15 different kinds of one hand weaps) this could get pretty insane ( having one weapon sprite sheet the same size as the character sprite sheet for each type of weapon )

Do you guys have any advice on how to implement this? (supporting overlaying the weapon sprites over the character sprites)

Thanks!

+8  A: 

Anchor points. Define anchor points for weapon graphics in each frame of your player sprite's graphics. Then define anchor points in your weapon sprites which match up to the weapon anchor points in your player sprite. Ensure that animation frames are synched between the player/weapon sprites.

leeor_net
I would like to note that this is basically the same way we do it in 3D :) Named bones can be attached to and moved through animation.
A.A. Grapsas
+4  A: 

When I did that for a platform shooting game, we had Anchor points (see leeor_net explanation), AND

My technique consisted of making the sprites in a way that I could rotate, or use frames that roatated around the single anchor point, in case of the weapon, and the character arm moved in a arc with a known radius. The result is that we could just ask to weapon to be at position(cos(angle)*radius, sin(angle)*radius); with angle being one of the possible angles that we have animation (like, 0, for forward, 90 for upward, and 45 for forward diagonal... if we had only these 3 positions). and "radius" being the radius in pixels from the center of the circle until the character arm.

This mathematical technique of positioning, also allow you to "offset" the weapon, by using anchor points, or manually summing a value on the code, etc... So you can "fix" errors (like, someone edited the weapon and moved it by accident 3 pixels downward... you can just sum +3 on y or move its anchor...).

In my game, the intention, and result (the technique worked flawlessy), is that the character pointed the gun at the mouse cursor, so I could shoot anywhere on the screen. To give a smooth animation, we had a frame each 10 degrees (thus 36 frames). If you game is pixel art instead, use angles that give less jagged lines (like 45, more or less 22.5, 90, 0...), you can have usually up to 16 directions when you want the "less jaggy lines" angles. (see a pixel art tutorial to see the reason for that, but basically, is making the line have a x/y ratio that is a integer, allowing regular pixel spacing without anti-alias to represent it...)

speeder