User prompt
fix the bug : 3rd platform moves before the ninja had arrived
User prompt
reorganize game.update in 1 responsibility functions
User prompt
randomize the platforms width between 100 and 400
Code edit (4 edits merged)
Please save this source code
User prompt
add groundOffset global variable and use it in createInitialPlatforms for platforms y
Code edit (6 edits merged)
Please save this source code
User prompt
rotate it when he starts falling
User prompt
Please fix the bug: 'TypeError: self is undefined' in or related to this line: 'self.ninjaStand.rotation = Math.PI; // Rotate the ninja by 180 degrees' Line Number: 236
User prompt
when ninja falls , rotate it by 180
Code edit (3 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: ninjaRun is not defined' in or related to this line: 'ninjaRun.visible = false; // Initially hide the running ninja graphic' Line Number: 27
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: ninjaRun is not defined' in or related to this line: 'if (ninjaRun.visible) {' Line Number: 218
Code edit (1 edits merged)
Please save this source code
User prompt
hide the ninjaRun as soon as the ninja stops walk on the stick
User prompt
set ninjaRun visble when moving
User prompt
in Ninja class, add a 2nd ninjaGraphics named ninjaRun
User prompt
Please fix the bug: 'ReferenceError: platforms is not defined' in or related to this line: 'var platformMinX = platforms[currentPlatformIndex + 1].x.x - platforms[currentPlatformIndex + 1].x.width / 2;' Line Number: 211
User prompt
remove the finishline notion
User prompt
for platforms remplace the use of an array by the use of 3 variables : platformCurrent platformTarget platformNext
User prompt
add the offsets to stick.x = platforms[currentPlatformIndex].x + platforms[currentPlatformIndex].width / 2; // Set stick to the right edge of the current platform stick.y = platforms[currentPlatformIndex].y - platforms[currentPlatformIndex].height; // Adjusted y-coordinate to ensure stick is on the platform too
User prompt
add an offset of -30 to the stick x position
User prompt
add an offset of 20 to the stick y position
User prompt
add an offset of 20 to the ninja y position
User prompt
move the code of foreground and midground from game.update to 'handleBackgroundMovement'
/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Ninja class var Ninja = Container.expand(function () { var self = Container.call(this); self.ninjaStand = self.attachAsset('ninja', { anchorX: 0.5, anchorY: 1.0 }); self.ninjaRun = self.attachAsset('ninjaRun', { anchorX: 0.5, anchorY: 1.0 }); self.ninjaRun.visible = false; // Initially hide the running ninja graphic self.update = function () { if (isNinjaMoving && !isStickFalling && ninja.x + ninja.width / 2 < stick.x + stick.length) { self.ninjaStand.visible = false; self.ninjaRun.visible = true; } else { self.ninjaStand.visible = true; self.ninjaRun.visible = false; } }; }); // Platform class var Platform = Container.expand(function () { var self = Container.call(this); var platformGraphics = self.attachAsset('platform', { anchorX: 0.5, anchorY: 1.0 }); platformGraphics.width = platformGraphics.width / 2; // Reduce the width of the platform self.update = function () { // Platform update logic }; }); // Stick class var Stick = Container.expand(function () { var self = Container.call(this); var stickGraphics = self.attachAsset('stick', { anchorX: 0.5, anchorY: 1.0 }); self.length = 0; self.update = function () { // Stick update logic }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Init game with sky blue background }); /**** * Game Code ****/ // Add background image var background = LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(background); // Initialize game variables var groundOffset = 2732 + 20; var ninja; var stick; var platformCurrent; var platformTarget; var platformNext; var isStickGrowing = false; var isStickFalling = false; var isNinjaMoving = false; var legsOffset = 50; var score = 0; var foreground1; var foreground2; var midground1; var midground2; // Create initial platforms function createInitialPlatforms() { var platform1 = new Platform(); platform1.x = 300; platform1.y = groundOffset; // Adjusted y-coordinate to touch the ground game.addChild(platform1); platformCurrent = platform1; var platform2 = new Platform(); var minDistance = 200; var maxDistance = 1400; var distance = Math.floor(Math.random() * (maxDistance - minDistance + 1)) + minDistance; platform2.x = platform1.x + platform1.width / 2 + distance; platform2.y = groundOffset; // Adjusted y-coordinate to touch the ground game.addChild(platform2); platformTarget = platform2; var platform3 = new Platform(); platform3.x = 2500; // Position the third platform outside the screen platform3.y = groundOffset; // Adjusted y-coordinate to touch the ground game.addChild(platform3); platformNext = platform3; } // Start the game function startGame() { game.addChildAt(background, 0); // Ensure background is behind all other game elements // Add midground images to create an infinite horizontal midground midground1 = LK.getAsset('midground', { anchorX: 0.5, anchorY: 1.0, x: 2048 / 2, y: 2732 }); game.addChild(midground1); midground2 = LK.getAsset('midground', { anchorX: 0.5, anchorY: 1.0, x: 2048 + 2048 / 2, y: 2732, scaleX: -1 }); game.addChild(midground2); createInitialPlatforms(); ninja = new Ninja(); if (platformCurrent) { ninja.x = platformCurrent.x; ninja.y = platformCurrent.y - platformCurrent.height + 20; // Adjusted y-coordinate to ensure ninja is visible on the first hill with an offset of 20 } else { console.error("platformCurrent is not defined"); } game.addChild(ninja); stick = new Stick(); stick.length = 0; // Set initial stick length to zero stick.height = stick.length; stick.x = platformCurrent.x + platformCurrent.width / 2 - 30; // Set stick to the right edge of the platform with an offset of -30 stick.y = platformCurrent.y - platformCurrent.height + 20; // Adjusted y-coordinate to ensure stick is on the platform with an offset of 20 game.addChild(stick); // Add foreground images to create an infinite horizontal foreground foreground1 = LK.getAsset('foreground', { anchorX: 0.5, anchorY: 1.0, x: 2048 / 2, y: 2732 }); game.addChild(foreground1); foreground2 = LK.getAsset('foreground', { anchorX: 0.5, anchorY: 1.0, x: 2048 + 2048 / 2, y: 2732, scaleX: -1 }); game.addChild(foreground2); } // Handle touch down event game.down = function (x, y, obj) { if (!isStickGrowing && !isStickFalling && !isNinjaMoving) { isStickGrowing = true; } }; // Handle touch up event game.up = function (x, y, obj) { if (isStickGrowing && !isNinjaMoving) { isStickGrowing = false; isStickFalling = true; } }; // Update game logic game.update = function () { if (isStickGrowing) { stick.length += 10; stick.height = stick.length; } ninja.update(); if (isStickFalling && !isNinjaMoving) { stick.rotation += 0.1; if (stick.rotation >= Math.PI / 2) { stick.rotation = Math.PI / 2; isStickFalling = false; isNinjaMoving = true; } } if (isNinjaMoving) { console.log("Moving...", "ninja.x= " + ninja.x, "stick.x= " + (stick.x + stick.length)); handleBackgroundMovement(); // Shift platforms, ninja, and stick to the left if (!isStickGrowing && !isStickFalling) { platformCurrent.x -= 5; platformTarget.x -= 5; platformNext.x -= 5; if (platformCurrent.x + platformCurrent.width / 2 < 0) { platformCurrent.x = 2048 + platformCurrent.width / 2; } if (platformTarget.x + platformTarget.width / 2 < 0) { platformTarget.x = 2048 + platformTarget.width / 2; } if (platformNext.x + platformNext.width / 2 < 0) { platformNext.x = 2048 + platformNext.width / 2; } } ninja.x -= 5; stick.x -= 5; // Make the ninja walk on the stick if (ninja.x < stick.x + stick.length) { ninja.x += 10; } else { console.log("Passed the stick", "Run= " + ninja.ninjaRun.visible, "Stand= " + ninja.ninjaStand.visible); // If the ninja has walked to the end of the stick if (platformTarget) { var ninjaMinX = ninja.x - ninja.width / 2 + legsOffset; var ninjaMaxX = ninja.x + ninja.width / 2 - legsOffset; var platformMinX = platformTarget.x - platformTarget.width / 2; var platformMaxX = platformTarget.x + platformTarget.width / 2; var platformMinX = platformTarget.x - platformTarget.width / 2; var platformMaxX = platformTarget.x + platformTarget.width / 2; if (ninjaMaxX < platformMinX || ninjaMinX > platformMaxX) { // If the stick does not reach the next platform or exceeds it, make the ninja fall ninja.y += 20; ninja.ninjaStand.rotation = Math.PI; // Rotate the ninja by 180 degrees if (ninja.y > 2732) { // If ninja falls off the screen LK.showGameOver(); } } else { // If the stick reaches the next platform, stop the ninja and reset the stick if (platformTarget.x - platformTarget.width / 2 <= 300) { isNinjaMoving = false; // Remove the first platform from the game and the platforms array game.removeChild(platformCurrent); platformCurrent = platformTarget; platformTarget = platformNext; score++; stick.length = 0; stick.height = 0; stick.rotation = 0; stick.x = platformCurrent.x + platformCurrent.width / 2 - 30; // Set stick to the right edge of the current platform with an offset of -30 stick.y = platformCurrent.y - platformCurrent.height + 20; // Adjusted y-coordinate to ensure stick is on the platform with an offset of 20 // Create a new platform to the right of the screen only if there are less than two platforms if (!platformNext) { var newPlatform = new Platform(); var minDistance = 200; var maxDistance = 1400; var distance = Math.floor(Math.random() * (maxDistance - minDistance + 1)) + minDistance; newPlatform.x = platformCurrent.x + platformCurrent.width / 2 + distance; newPlatform.y = 2732; // Adjusted y-coordinate to touch the ground game.addChild(newPlatform); platformNext = newPlatform; } } } } } } }; // Start the game startGame(); function handleBackgroundMovement() { // Move midground images to the left midground1.x -= 7; midground2.x -= 7; // Move foreground images to the left foreground1.x -= 10; foreground2.x -= 10; // Reset midground position to create an infinite scrolling effect if (midground1.x + 2048 / 2 < 0) { midground1.x = midground2.x + 2048; } if (midground2.x + 2048 / 2 < 0) { midground2.x = midground1.x + 2048; } // Reset foreground position to create an infinite scrolling effect if (foreground1.x + 2048 / 2 < 0) { foreground1.x = foreground2.x + 2048; } if (foreground2.x + 2048 / 2 < 0) { foreground2.x = foreground1.x + 2048; } }
===================================================================
--- original.js
+++ change.js
@@ -67,9 +67,9 @@
y: 2732 / 2
});
game.addChild(background);
// Initialize game variables
-var groundOffset = 2732;
+var groundOffset = 2732 + 20;
var ninja;
var stick;
var platformCurrent;
var platformTarget;