/****
* Classes
****/
var Background = Container.expand(function () {
var self = Container.call(this);
var backgroundGraphics = self.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 1;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.y = 0;
}
};
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.gravity = 0.5;
self.jumpForce = -15;
self.grounded = false;
self.update = function () {
self.y += self.speed;
self.x += self.speedX;
scoreTxt.setText(score);
if (LK.ticks % 180 == 0) {
var newThorns = new Thorns();
newThorns.spawn(Math.random() < 0.5 ? 0 : 2048, 0);
game.addChild(newThorns);
thornsArray.push(newThorns);
}
playerGraphics.scale.x = self.speedX > 0 ? 1 : -1;
self.speed += self.gravity;
if (self.x <= 0 + playerGraphics.width / 2) {
self.x = 0 + playerGraphics.width / 2;
self.speedX = 20;
score += 1;
scoreTxt.setText(score);
} else if (self.x >= 2048 - playerGraphics.width / 2) {
self.x = 2048 - playerGraphics.width / 2;
self.speedX = -20;
score += 1;
scoreTxt.setText(score);
}
if (self.y > 2732 - playerGraphics.height / 2) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
for (var i = 0; i < game.children.length; i++) {
if (game.children[i] instanceof Thorns && self.intersects(game.children[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};
self.jump = function () {
self.speed = self.jumpForce;
self.grounded = false;
LK.getSound('jump').play();
};
});
var Thorns = Container.expand(function () {
var self = Container.call(this);
var thornsGraphics = self.attachAsset('thorns', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732 + thornsGraphics.height / 2) {
self.destroy();
var index = thornsArray.indexOf(self);
if (index > -1) {
thornsArray.splice(index, 1);
}
}
};
self.spawn = function (x, y) {
self.x = x;
self.y = y;
if (x == 0) {
thornsGraphics.scale.x = 1;
} else {
thornsGraphics.scale.x = -1;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x66b5e4
});
/****
* Game Code
****/
var thornsArray = [];
game.down = function (x, y, obj) {
player.jump();
};
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var background = game.addChild(new Background());
background.x = 1024;
background.y = 1366;
var player = game.addChild(new Player());
var score = 0;
player.x = 1024;
player.y = 1366;
player.speedX = -20;