/****
* Classes
****/
// Marshmallow character class
var Marshmallow = Container.expand(function () {
var self = Container.call(this);
var marshmallowGraphics = self.createAsset('marshmallow', 'Fluffy marshmallow character', 0.5, 1);
self.velocity = {
x: 0,
y: 0
};
self.isJumping = false;
self.jump = function () {
if (!self.isJumping) {
self.velocity.y = -15;
self.isJumping = true;
}
};
self.update = function () {
self.y += self.velocity.y;
self.velocity.y += 0.5; // gravity effect
if (self.y > game.height - marshmallowGraphics.height) {
self.y = game.height - marshmallowGraphics.height;
self.isJumping = false;
}
};
});
// Trap class for obstacles
var Trap = Container.expand(function () {
var self = Container.call(this);
var trapGraphics = self.createAsset('trap', 'Tasty trap obstacle', 0.5, 1);
self.speed = 5;
self.move = function () {
self.x -= self.speed;
};
self.isOffScreen = function () {
return self.x < -trapGraphics.width;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xFFFFFF // Init game with white background
});
/****
* Game Code
****/
var marshmallow = game.addChild(new Marshmallow());
marshmallow.x = game.width / 4;
marshmallow.y = game.height - 100;
var traps = [];
var scoreTxt = new Text2('0', {
size: 150,
fill: "#000000"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.on('down', function (obj) {
marshmallow.jump();
});
var spawnTrap = function () {
var trap = new Trap();
trap.x = game.width;
trap.y = game.height - 50;
traps.push(trap);
game.addChild(trap);
};
var updateScore = function () {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
};
var gameOver = function () {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
};
LK.on('tick', function () {
marshmallow.update();
for (var i = traps.length - 1; i >= 0; i--) {
traps[i].move();
if (traps[i].isOffScreen()) {
traps[i].destroy();
traps.splice(i, 1);
} else if (marshmallow.intersects(traps[i])) {
gameOver();
}
}
if (LK.ticks % 120 == 0) {
spawnTrap();
}
if (LK.ticks % 60 == 0) {
updateScore();
}
});