/**** * Classes ****/ // Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { self.x += self.speed; }; }); // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); }); // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize assets used in this game. var player = game.addChild(new Player()); player.x = 1024; player.y = 2732; var enemy = game.addChild(new Enemy()); enemy.x = 0; enemy.y = Math.random() * 2732; var obstacles = []; for (var i = 0; i < 10; i++) { var obstacle = game.addChild(new Obstacle()); obstacle.x = Math.random() * 2048; obstacle.y = Math.random() * 2732; obstacles.push(obstacle); } game.down = function (x, y, obj) { player.y -= 100; }; game.update = function () { if (player.intersects(enemy) || obstacles.some(obstacle => player.intersects(obstacle))) { LK.showGameOver(); } };
/****
* Classes
****/
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.x += self.speed;
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize assets used in this game.
var player = game.addChild(new Player());
player.x = 1024;
player.y = 2732;
var enemy = game.addChild(new Enemy());
enemy.x = 0;
enemy.y = Math.random() * 2732;
var obstacles = [];
for (var i = 0; i < 10; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = Math.random() * 2048;
obstacle.y = Math.random() * 2732;
obstacles.push(obstacle);
}
game.down = function (x, y, obj) {
player.y -= 100;
};
game.update = function () {
if (player.intersects(enemy) || obstacles.some(obstacle => player.intersects(obstacle))) {
LK.showGameOver();
}
};