/**** * 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.slideTo(self.x - 300); // Slide to the left lane } }; // Method to move the runner right self.moveRight = function () { if (self.lane < 2) { self.lane++; self.slideTo(self.x + 300); // Slide to the right lane } }; // Method to slide the runner to a new x position self.slideTo = function (newX) { var slideSpeed = 20; // Adjust slide speed as needed var slideInterval = LK.setInterval(function () { if (Math.abs(self.x - newX) < slideSpeed) { self.x = newX; LK.clearInterval(slideInterval); } else { self.x += self.x < newX ? slideSpeed : -slideSpeed; } }, 16); // Approximately 60 FPS }; // 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 < game.width / 2) { 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(); } };
===================================================================
--- original.js
+++ change.js
@@ -30,18 +30,30 @@
// Method to move the runner left
self.moveLeft = function () {
if (self.lane > 0) {
self.lane--;
- self.x -= 300; // Move to the left lane
+ self.slideTo(self.x - 300); // Slide 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
+ self.slideTo(self.x + 300); // Slide to the right lane
}
};
+ // Method to slide the runner to a new x position
+ self.slideTo = function (newX) {
+ var slideSpeed = 20; // Adjust slide speed as needed
+ var slideInterval = LK.setInterval(function () {
+ if (Math.abs(self.x - newX) < slideSpeed) {
+ self.x = newX;
+ LK.clearInterval(slideInterval);
+ } else {
+ self.x += self.x < newX ? slideSpeed : -slideSpeed;
+ }
+ }, 16); // Approximately 60 FPS
+ };
// Update method for runner
self.update = function () {
// Add any continuous updates for the runner here
};