Click or drag to resize

SpriteControllerDoTick Event

The function called by the timer every 10 millisecods This is usually where you will do the majority of the work. You can define this manually, or when you instantiate the SpriteController

Namespace:  SpriteLibrary
Assembly:  SpriteLibrary (in SpriteLibrary.dll) Version: 1.0.0.6 (1.0.0.6)
Syntax
C#
public event EventHandler DoTick

Value

Type: SystemEventHandler
Examples
The Sprite controller uses a System.Windows.Forms.Timer. This timer is notoriously un-precise, but it is very easy to set up initially. It tries to fire off every 10 milliseconds, but it can fire off incredibly slowly if you have long pieces of code; the DoTick function needs to finish before it can start again. You want all your functions to run as quickly as possible to avoid things looking jerky. Most programs you will make using the sprite library will begin by tapping into the DoTick Event. Every time the sprite controller is ready to pass control back to your program, it will call the DoTick event. You want to see if you should be doing anything, and then exiting the do-tick function.
C#
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;
       }
   }
See Also