/**** * Classes ****/ // Define the Obstacle class 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 the obstacle self.update = function () { self.x -= self.speed; if (self.x < -100) { // Assuming obstacle is off-screen at x < -100 self.destroy(); } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Define the Runner class 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.jumpHeight = 200; self.isJumping = false; self.gravity = 5; self.velocityY = 0; // Update method for the runner self.update = function () { if (self.isJumping) { self.velocityY -= self.gravity; self.y -= self.velocityY; if (self.y >= 200) { // Assuming ground level is at y = 200 self.y = 200; self.isJumping = false; self.velocityY = 0; } } }; // Method to make the runner jump self.jump = function () { if (!self.isJumping) { self.isJumping = true; self.velocityY = self.jumpHeight; } }; }); /**** * 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 = 200; runner.y = 200; // Array to hold obstacles var obstacles = []; // Function to spawn obstacles function spawnObstacle() { var obstacle = new Obstacle(); obstacle.x = 2048; // Start off-screen to the right obstacle.y = 200; // Ground level obstacles.push(obstacle); game.addChild(obstacle); } // Handle game updates game.update = function () { runner.update(); // Update obstacles for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); if (runner.intersects(obstacles[i])) { // Handle collision LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Spawn new obstacles periodically if (LK.ticks % 120 === 0) { // Every 2 seconds at 60 FPS spawnObstacle(); } }; // Handle touch events for jumping game.down = function (x, y, obj) { runner.jump(); };
/****
* Classes
****/
// Define the Obstacle class
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 the obstacle
self.update = function () {
self.x -= self.speed;
if (self.x < -100) {
// Assuming obstacle is off-screen at x < -100
self.destroy();
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define the Runner class
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.jumpHeight = 200;
self.isJumping = false;
self.gravity = 5;
self.velocityY = 0;
// Update method for the runner
self.update = function () {
if (self.isJumping) {
self.velocityY -= self.gravity;
self.y -= self.velocityY;
if (self.y >= 200) {
// Assuming ground level is at y = 200
self.y = 200;
self.isJumping = false;
self.velocityY = 0;
}
}
};
// Method to make the runner jump
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.velocityY = self.jumpHeight;
}
};
});
/****
* 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 = 200;
runner.y = 200;
// Array to hold obstacles
var obstacles = [];
// Function to spawn obstacles
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048; // Start off-screen to the right
obstacle.y = 200; // Ground level
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Handle game updates
game.update = function () {
runner.update();
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (runner.intersects(obstacles[i])) {
// Handle collision
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Spawn new obstacles periodically
if (LK.ticks % 120 === 0) {
// Every 2 seconds at 60 FPS
spawnObstacle();
}
};
// Handle touch events for jumping
game.down = function (x, y, obj) {
runner.jump();
};