Skip to content

Instantly share code, notes, and snippets.

@casarock
Created March 3, 2014 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save casarock/9334781 to your computer and use it in GitHub Desktop.
Save casarock/9334781 to your computer and use it in GitHub Desktop.
Acc.Game = function (game) {
this.game = game;
};
Acc.Game.prototype = {
create: function () {
this.MAX_VELX = 125;
this.game.stage.backgroundColor = '#dddddd';
this.platforms = this.game.add.group();
this.platforms.createMultiple(10, 'platform');
this.player = this.add.sprite(10, 400, 'player');
this.player.body.collideWorldBounds = false;
this.player.body.velocity.y = 50;
this.player.body.mass = 0;
this.player.body.immovable = false;
this.player.body.rebound = false;
this.velX = 0;
this.cursors = this.game.input.keyboard.createCursorKeys();
this.timer = this.game.time.events.loop(1500, this.addPlatform, this);
},
update: function () {
if (this.cursors.left.isDown) {
this.velX -= 5;
this.velX = this.velX > -this.MAX_VELX ? this.velX : -this.MAX_VELX;
}
if (this.cursors.right.isDown) {
this.velX += 5;
this.velX = this.velX < this.MAX_VELX ? this.velX : this.MAX_VELX;
}
this.player.body.velocity.x = this.velX;
if (this.player.y > this.game.world.height + 32) {
this.player.y = 100;
this.player.x = 10;
this.velX = 0;
this.player.body.velocity.setTo(0, 50);
}
if (this.player.x > this.game.world.width + 16) {
this.player.x = -16;
}
if (this.player.x < - 16) {
this.player.x = this.game.world.width + 16;
}
this.game.physics.collide(this.platforms, this.player, this.collideCallback, null, this);
},
collideCallback: function (platform, player) {
},
addPlatform: function() {
var platform = this.platforms.getFirstDead();
platform.reset(50, this.game.world.height + 16);
platform.scale.setTo(0.25, 0.5);
platform.outOfBoundsKill = true;
platform.body.checkCollision.up = true;
platform.body.checkCollision.down = false;
platform.body.immovable = true;
platform.body.velocity.y = -100;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment