SpriteControllerDoTick Event |
Namespace: SpriteLibrary
public partial class ShootingFieldForm : Form { public ShootingFieldForm() { InitializeComponent(); MainDrawingArea.BackgroundImage = Properties.Resources.Background; MainDrawingArea.BackgroundImageLayout = ImageLayout.Stretch; MySpriteController = new SpriteController(MainDrawingArea, CheckForKeyPress); } private void CheckForKeyPress(object sender, EventArgs e) { bool left = false; bool right = false; bool space = false; bool didsomething = false; TimeSpan duration = DateTime.Now - LastMovement; if (duration.TotalMilliseconds < 100) return; LastMovement = DateTime.Now; if (MySpriteController.IsKeyPressed(Keys.A) || MySpriteController.IsKeyPressed(Keys.Left)) { left = true; } if (MySpriteController.IsKeyPressed(Keys.D)||MySpriteController.IsKeyPressed(Keys.Right)) { right = true; } if (left && right) return; //do nothing if we conflict if (left) { if (LastDirection != MyDir.left) { Spaceship.SetSpriteDirectionDegrees(180); //We want to only change animation once. Every time we change //the animation, it starts at the first frame again. Spaceship.ChangeAnimation(0); LastDirection = MyDir.left; } didsomething = true; Spaceship.MovementSpeed = 15; Spaceship.AutomaticallyMoves = true; } if (right) { if (LastDirection != MyDir.right) { Spaceship.SetSpriteDirectionDegrees(0); Spaceship.ChangeAnimation(0); LastDirection = MyDir.right; } didsomething = true; Spaceship.AutomaticallyMoves = true; Spaceship.MovementSpeed = 15; } if(!didsomething) { LastDirection = MyDir.stopped; //No keys pressed. Stop moving Spaceship.MovementSpeed = 0; } }