Sunday, 18 October 2015

Obstacles - Simplegamelib Part Eight

This is the eighth 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. There's been some refactoring going on, documentation being updated and general bug fixing.

This time we are looking making sprites solid to they don't just float over each other. This little ninja is going to hit a brick wall!

To achieve this, we set the obstacles property which is a SpriteGroup with anything the player is not allowed to walk over. The wall Sprite is created like any other and added to this group. That's it!

  Game game = new Game("My Game", '#surface');
  SpriteGroup walls = new SpriteGroup();
  player = game.createSprite("images/ninjadude.png", 48, 48);
  wall = game.createSprite("images/brick.png", 100, 100);

  game.player.sprite = player;
  walls.canvas = player.canvas;
  walls.add(wall);

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

  wall..position = new Point(150, 0);

  print('starting game...');
  game
    ..setUpKeys()
    ..start();

Click here to see it in action. Use the cursor keys to bump into the wall.

Next time, we will look at creating some collectible items.