Monday, October 19, 2015

architecture - Player sprite animation in 2D platform game


It's pretty easy to get animations rolling from a sprite sheet. Though which ways there are on combining the animations with player dynamics and motion?


I've figured there's two starting points anyway:



  • Player animation contains motion (the animation slab defines how the sprite moves)

  • Player animation does not contain motion



Simple state machine might do the stuff, but are there alternatives? How would you write the state machine itself for doing it?


I'm writing a small hack&slash&jump platform game in html5. The player should stay in place when he strikes a sword, perhaps even move a bit (one can accomplish it through a hack), but otherwise move pretty much without limits. I've got couple frames and I'd like it all to mesh together well.



Answer



I prefer to keep animation and motion separate, since it provides for more flexibility.


In my game engine I have a concept called an Action Matrix, which is basically an array of arrays of integers, which are index references into the sprite sheet. Each sub-array is a list of numbers which define the frames of an animation, and each character is assigned an action matrix.


The movement code is hard-wired to use the first four actions for the animation to be played while the character is moving, (in the four cardinal directions,) and beyond that you can create custom animations which can be invoked by playing a different action from the action matrix. These can be used with or without also moving the sprite's position, which is controlled by a separate command.


Also, by playing one of the first four actions in a loop with no movement, you can get a character who seems to be walking in place, or a flying/floating vehicle that just hovers there, or various other other versions of an "idle" animation.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...