/****
* Classes
****/
// Class for Obstacles
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.x += self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the Runner character
var Runner = Container.expand(function () {
var self = Container.call(this);
var runnerGraphics = self.attachAsset('runner', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
// Logic for runner movement
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize runner
var runner = game.addChild(new Runner());
runner.x = 2048 / 2;
runner.y = 2732 - 200;
// Array to keep track of obstacles
var obstacles = [];
// Function to handle runner movement
function handleMove(x, y, obj) {
runner.x = x;
}
// Event listener for touch/mouse move
game.move = handleMove;
// Function to spawn obstacles
function spawnObstacle() {
var newObstacle = new Obstacle();
newObstacle.x = 2048;
newObstacle.y = 2732 - 200;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
// Set interval to spawn obstacles
var obstacleInterval = LK.setInterval(spawnObstacle, 2000);
// Update function for game logic
game.update = function () {
for (var i = obstacles.length - 1; i >= 0; i--) {
if (runner.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};
// Clean up interval on game over
game.on('gameOver', function () {
LK.clearInterval(obstacleInterval);
});