Friday, 16 October 2015

Sprite Speed and Limits - Simplegamelib Part Seven

This is the seventh part in an ongoing series where we will look at the package I am currently writing called simplegamelib. Development is usually a little ahead of the current blog post. The projects homepage is located here.

We have already seen how to get sprites on the screen moving about. Sometimes it is useful to speed up a particular Sprite, whilst this could be done with setting a higher movement value, it is also possible to set a speed value that acts as a multiplier. This is useful, for example, in a game where the Player picks up a speed up bonus. Once the power-up has run out the speed can be set back to 1 and we don't need to worry about direction either.

  Game game = new Game("My Game", '#surface');

  player = game.createSprite("images/ninjadude.png", 48, 48);
  dude = game.createSprite("images/ninjadude.png", 48, 48);

  player
    ..position = new Point(0, 10)
    ..movement = Movements.east
    ..speed = 3;

  dude
    ..position = new Point(10, 10)
    ..movement = new Point(1, 1)
    ..limits = new Rectangle(0, 0, 133, 133);

  print('starting game...');

  game.start();

The other aspect we want to address is limiting a Sprite's position on the screen. Imagine a Space Invaders type game where the player's sprite is restricted to the lower portion of the screen. To achieve this for one of our ninja's, we set the limit property to desired rectangular area.

Click here to see it in action. Hit reload to start again. One Ninja will zoom off never to be seen again. The other will head diagonally down the screen until it hits an invisible wall. These things happen to ninjas.

Next time, we will look at making sprites solid to they don't just float over each other.