/****
* 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;
// Update method for obstacle
self.update = function () {
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
// Class for the main character (runner)
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.lane = 1; // Start in the middle lane
// Method to move the runner left
self.moveLeft = function () {
if (self.lane > 0) {
self.lane--;
self.x -= 300; // Move to the left lane
}
};
// Method to move the runner right
self.moveRight = function () {
if (self.lane < 2) {
self.lane++;
self.x += 300; // Move to the right lane
}
};
// Update method for runner
self.update = function () {
// Add any continuous updates for the runner here
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize runner
var runner = new Runner();
runner.x = 1024; // Center lane
runner.y = 2300; // Near the bottom of the screen
game.addChild(runner);
// Array to hold obstacles
var obstacles = [];
// Function to create a new obstacle
function createObstacle() {
var obstacle = new Obstacle();
obstacle.x = 1024 + (Math.floor(Math.random() * 3) - 1) * 300; // Random lane
obstacle.y = -100; // Start above the screen
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Handle touch events for moving the runner
game.down = function (x, y, obj) {
if (x < 1024) {
runner.moveLeft();
} else {
runner.moveRight();
}
};
// Game update loop
game.update = function () {
// Update runner
runner.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (obstacles[i].intersects(runner)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Create new obstacles periodically
if (LK.ticks % 60 == 0) {
createObstacle();
}
};