/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Dart class
var Dart = Container.expand(function () {
var self = Container.call(this);
var dartGraphics = self.attachAsset('dart', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.update = function () {
self.y += self.speed;
};
});
// Target class
var Target = Container.expand(function () {
var self = Container.call(this);
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize score
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize target
var target = game.addChild(new Target());
target.x = 2048 / 2;
target.y = 2732 / 4;
// Initialize darts array
var darts = [];
// Mouse or touch down on the game
game.down = function (x, y, obj) {
var newDart = new Dart();
newDart.x = x;
newDart.y = 2732 - 100; // Start from the bottom of the screen
darts.push(newDart);
game.addChild(newDart);
};
// Update game every tick
game.update = function () {
for (var i = darts.length - 1; i >= 0; i--) {
if (darts[i].intersects(target)) {
// Update score
score += 1;
scoreTxt.setText(score);
darts[i].destroy();
darts.splice(i, 1);
} else if (darts[i].y < -50) {
darts[i].destroy();
darts.splice(i, 1);
}
}
};