/****
* Classes
****/
// The assets will be automatically created and loaded by the LK engine.
// Dinosaur class
var Dinosaur = Container.expand(function () {
var self = Container.call(this);
var dinosaurGraphics = self.attachAsset('dinosaur', {
anchorX: 0.5,
anchorY: 1
});
self.y = 2732 - dinosaurGraphics.height;
self.jump = function () {
self.speed = -30;
};
self.update = function () {
self.speed += 1;
self.y += self.speed;
if (self.y > 2732 - dinosaurGraphics.height) {
self.y = 2732 - dinosaurGraphics.height;
self.speed = 0;
}
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1
});
self.x = 2048;
self.y = 2732 - obstacleGraphics.height;
self.speed = -10;
self.update = function () {
self.x += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
var dinosaur = game.addChild(new Dinosaur());
var obstacles = [];
game.down = function (x, y, obj) {
dinosaur.jump();
};
game.update = function () {
if (LK.ticks % 120 == 0) {
var obstacle = new Obstacle();
obstacles.push(obstacle);
game.addChild(obstacle);
}
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].x < -obstacles[i].width) {
obstacles[i].destroy();
obstacles.splice(i, 1);
} else if (dinosaur.intersects(obstacles[i])) {
LK.showGameOver();
}
}
};