Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created March 5, 2014 14:32
Show Gist options
  • Save tmcw/9368261 to your computer and use it in GitHub Desktop.
Save tmcw/9368261 to your computer and use it in GitHub Desktop.

Mission: Losing the Game

Let's figure out how to lose the game. Right now you can see what happens when you collide with something: something like

collision: function(other) {
  other.center.y = this.center.y; // follow the player
  other.center.x = this.center.x; // follow the player
}

Or something you've tricked out. How could we add a 'lose' state?

First: the alert function. This pops up one of those annoying boxes you have to click okay.

collision: function(other) {
  // this line pops up a window that says 'you lose'
  alert('you lose!');
  // this one stops the game
  this.c.ticker.stop();
  // this is like hitting refresh in your browser
  location.reload();
}

Mission: Powerups

Next: you've added bonus characters to your game. Right now running into them just makes you grab them and take them with you. Let's turn them into power-ups that help you run faster.

First question: how fast can you run? You've already figured this out, and already changed it!

if (this.c.inputter.isDown(this.c.inputter.LEFT_ARROW)) {
  // move by 2 pixels!
  this.center.x -= 2;
}

Let's say 2 pixels are the speed. Now how do we make power-ups make us faster? Well, we've been doing things with the collision function - how about putting it in there?

First - a person needs to have a speed to start out. First find the line that says

this.size = { x:9, y:9 };

Let's add a speed:

this.size = { x:9, y:9 };
this.speed = 2;

Cool! Now let's use it a little:

if (this.c.inputter.isDown(this.c.inputter.LEFT_ARROW)) {
  // move by 2 pixels!
  this.center.x -= this.speed;
}

And finally, let's increase it when you collide with something:

collision: function(other) {
  this.speed = this.speed + 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment