I have a sprite sheet that contains a character walk cycle, and I have made a player class that allows the player to move and cycle through the walk animation, however the animation only works whilst walking to the right. I use the arrow keys to move up, down, left and right, meaning when I press any key, my player moves in the correct direction, but only performs the animation of walking to the right.
The current animation that works, has been called like this in the LoadContent()
function:
spritesheet = Content.Load<Texture2D>("sprites2");
player = new Player(spritesheet, new Rectangle(0, 96, 32, 48),
new Vector2(100, 100), 4);
This does its job, as it calls each 4 sprites to complete the walking animation to the right, and new Rectangle(0,96,32,48)
is the location cut out from the sprite sheet. However I need (for example) new Rectangle(30, 101, 65, 52)
to be used also, which will correctly animate my character when the left arrow key has been pressed.
My question is, how would I get another location of the sprite sheet loaded, and then actually work when I press the correct key?
This is a very difficult question to ask and I very much apologize if it was difficult to understand. Accepting all edits to my question, and thank you in advanced.
I think a better way would be to pass the entire sprite sheet (or the entire section of the sprite sheet that contains the player's sprite) to the Player
class and have a separate animation class handle how to select rectangles from the sheet. This is easier if you have frames that are all the same size, but it's doable if you don't.
In the animation class, you can set up a List<Rectangle>
for each animation, MoveUp, MoveDown, MoveLeft, MoveRight
, etc. and then call a method PlayAnimation()
from your Player
class whenever your player is performing one of those actions.
If you need each frame to have more information than just which Rectangle
to display (for example, if frames have varying times), you can create a Frame class to hold that info.