/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Bike class
var Bike = Container.expand(function () {
var self = Container.call(this);
var bikeGraphics = self.attachAsset('bike', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Update bike position or other properties
};
});
// Coin class
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var bike = game.addChild(new Bike());
bike.x = 2048 / 2;
bike.y = 2732 - 200;
var obstacles = [];
var coins = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function handleMove(x, y, obj) {
bike.x = x;
}
game.move = handleMove;
game.update = function () {
if (LK.ticks % 60 == 0) {
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
var newCoin = new Coin();
newCoin.x = Math.random() * 2048;
newCoin.y = -50;
coins.push(newCoin);
game.addChild(newCoin);
}
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].intersects(bike)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
for (var j = coins.length - 1; j >= 0; j--) {
if (coins[j].intersects(bike)) {
score += 1;
scoreTxt.setText(score);
coins[j].destroy();
coins.splice(j, 1);
}
}
};