// Hero character
LK.init.shape('hero', {width: 100, height: 100, color: 0x00FF00, shape: 'ellipse'});
// Obstacle
LK.init.shape('obstacle', {width: 80, height: 80, color: 0xFF0000, shape: 'box'});
// Coin
LK.init.shape('coin', {width: 50, height: 50, color: 0xFFFF00, shape: 'ellipse'});
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function (y) {
self.y = y;
};
});
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.move = function () {
self.x -= self.speed;
};
});
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.move = function () {
self.x -= self.speed;
};
});
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
var hero = game.addChild(new Hero());
hero.x = 200;
hero.y = 2732 / 2;
var obstacles = [];
var coins = [];
var score = 0;
var scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
hero.move(pos.y);
});
LK.on('tick', function () {
// Move obstacles and check for collision
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
if (obstacles[i].x < -50) {
obstacles[i].destroy();
obstacles.splice(i, 1);
} else if (hero.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Move coins and check for collection
for (var j = coins.length - 1; j >= 0; j--) {
coins[j].move();
if (coins[j].x < -50) {
coins[j].destroy();
coins.splice(j, 1);
} else if (hero.intersects(coins[j])) {
score += 10;
scoreTxt.setText(score.toString());
coins[j].destroy();
coins.splice(j, 1);
}
}
// Spawn obstacles and coins
if (LK.ticks % 120 == 0) {
var newObstacle = new Obstacle();
newObstacle.x = 2048;
newObstacle.y = Math.random() * 2732;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
if (LK.ticks % 180 == 0) {
var newCoin = new Coin();
newCoin.x = 2048;
newCoin.y = Math.random() * 2732;
coins.push(newCoin);
game.addChild(newCoin);
}
});