patterncsharpModerate
Sprite animation handler
Viewed 0 times
handleranimationsprite
Problem
When a user presses a key in my game, their avatar can move down, left, right or up. By move, I mean their avatar animates while various other things move around them on the screen. This is accomplished by raising an event from the
The
When the
InputHandler class.public class InputHandler
{
public delegate void ActionListener(Actions action);
public event ActionListener ActionRequested;
public void ProcessKeyboard(KeyboardState keyboardState)
{
var keys = keyboardState.GetPressedKeys().ToList();
if (!keys.Any())
{
ActionRequested(Actions.Idle);
return;
}
foreach (var action in keys.Select(GetAction))
{
ActionRequested(action);
}
}
}The
Player class listens for the ActionRequested event:public class Player
{
private readonly IEnumerable sprites;
public Player(IEnumerable sprites)
{
this.sprites = sprites;
}
public void Listen(Actions action)
{
switch (action)
{
case Actions.Idle:
Idle();
return;
case Actions.MoveDown:
case Actions.MoveLeft:
case Actions.MoveRight:
case Actions.MoveUp:
Animate(action);
return;
}
}
public void Idle()
{
foreach (var sprite in sprites)
{
sprite.Idle();
}
}
public void Animate(Actions action)
{
foreach (var sprite in sprites)
{
sprite.Animate(action);
}
}
}When the
Animate method is called on the Sprite class, it gets the Animation for that Action (MoveDown, MoveLeft, etc.), gets the next frame from that Animation and resets the previous animation (so it doesn't start halfway through the next time the button is pressed). The Idle method moves the frame to the left-most position in the sprite sheet (so the avatar Solution
Looks pretty neat overall, I merely glanced at your code (favorited, I want to look at this more deeply, as I'm [playing with | learning] XNA myself, your XNA posts are very useful!).
One thing I've noticed, I don't think you need this
Instead, I'd probably do this:
...and in
Also in
One thing I've noticed, I don't think you need this
switch block:public void Listen(Actions action)
{
switch (action)
{
case Actions.Idle:
Idle();
return;
case Actions.MoveDown:
case Actions.MoveLeft:
case Actions.MoveRight:
case Actions.MoveUp:
Animate(action);
return;
}
}Instead, I'd probably do this:
public void Listen(Actions action)
{
Animate(action);
}...and in
Animate(Actions) I'd add the call to Idle() and exit if (action == Actions.Idle). "Being idle" does involve choosing a location on the sprite sheet, I don't see why it would be handled in Listen when everything else is in Animate.Also in
switch blocks you should use break;, and let the function return by itself.Code Snippets
public void Listen(Actions action)
{
switch (action)
{
case Actions.Idle:
Idle();
return;
case Actions.MoveDown:
case Actions.MoveLeft:
case Actions.MoveRight:
case Actions.MoveUp:
Animate(action);
return;
}
}public void Listen(Actions action)
{
Animate(action);
}Context
StackExchange Code Review Q#45901, answer score: 11
Revisions (0)
No revisions yet.