Monday, 5 October 2015

Sprite Priority - Simplegamelib Part Three

This is the third part in an ongoing series where we will look at the package called simplegamelib

This time we will create a little forest of trees to walk down the middle of. Two trees will be in the foreground and two in the background. This is achieved by placing the ninja sprite in the SpriteGroup after the background sprites but before the foreground. When the ninja goes off screen, the order of the sprites is reversed by calling the reverse method. The display will not look as good the second time around as the foreground will be drawn first but it does clearly show the order the drawing of the sprites SpriteGroup.

Sprite player;
Sprite backTree, backTree2;
Sprite foreTree, foreTree2;
Game game = new Game("My Game", '#surface');

void main() {

  backTree = game.createSprite("images/ftree.png");
  backTree2 = game.createSprite("images/ftree.png");
  player = game.createSprite("images/ninjadude.png");
  foreTree = game.createSprite("images/ftree.png");
  foreTree2 = game.createSprite("images/ftree.png");

    ..position = new Point(0, 30)
    ..movement = Movements.east;

  backTree.position = new Point(200, 20);
  backTree2.position = new Point(20, 20);

  foreTree.position = new Point(180, 60);
  foreTree2.position = new Point(40, 60);

  game.customUpdate = priorityDemo;
  print('starting game...');


void priorityDemo() {
  if (player.x > 333) {
    player.x = -50;

Click here to see it in action.

Next time, we will look at adding some keyboard controls so we can start getting interactive!