/**** * Classes ****/ // Define the Floor class var Floor = Container.expand(function () { var self = Container.call(this); var floorGraphics = self.attachAsset('floor', { anchorX: 0.5, anchorY: 0.5 }); }); // 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; self.update = function () { self.x -= self.speed; if (self.x < -self.width) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // Define the 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 = 10; self.jumpSpeed = -30; self.gravity = 1; self.isJumping = false; self.isFalling = false; self.update = function () { if (self.isJumping) { self.y += self.jumpSpeed; self.jumpSpeed += self.gravity; if (self.jumpSpeed >= 0) { self.isJumping = false; self.isFalling = true; } } else if (self.isFalling) { self.y += self.jumpSpeed; self.jumpSpeed += self.gravity; if (self.y >= game.groundLevel) { self.y = game.groundLevel; self.isFalling = false; self.jumpSpeed = -30; } } }; self.jump = function () { if (!self.isJumping && !self.isFalling) { self.isJumping = true; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Initialize game variables game.groundLevel = 2500; game.obstacles = []; game.score = 0; // Create floor instance var floor = game.addChild(new Floor()); floor.x = 2048 / 2; floor.y = game.groundLevel + 25; // Adjust y position to align with ground level // Create player instance var player = game.addChild(new Player()); player.x = 200; player.y = game.groundLevel; // Create score text var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to handle player jump game.down = function (x, y, obj) { player.jump(); }; // Function to spawn obstacles function spawnObstacle() { var obstacle = new Obstacle(); obstacle.x = 2048; obstacle.y = game.groundLevel; game.obstacles.push(obstacle); game.addChild(obstacle); } // Update game state game.update = function () { player.update(); for (var i = game.obstacles.length - 1; i >= 0; i--) { game.obstacles[i].update(); if (player.intersects(game.obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } if (LK.ticks % 60 == 0) { spawnObstacle(); game.score++; scoreTxt.setText(game.score); } };
===================================================================
--- original.js
+++ change.js
@@ -1,7 +1,15 @@
/****
* Classes
****/
+// Define the Floor class
+var Floor = Container.expand(function () {
+ var self = Container.call(this);
+ var floorGraphics = self.attachAsset('floor', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+});
// Define the Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
@@ -67,8 +75,12 @@
// Initialize game variables
game.groundLevel = 2500;
game.obstacles = [];
game.score = 0;
+// Create floor instance
+var floor = game.addChild(new Floor());
+floor.x = 2048 / 2;
+floor.y = game.groundLevel + 25; // Adjust y position to align with ground level
// Create player instance
var player = game.addChild(new Player());
player.x = 200;
player.y = game.groundLevel;