Code edit (3 edits merged)
Please save this source code
User prompt
when isReturningToBase ,platformNext.x should not go below 2048-platformNext.width
User prompt
prevent stick to grow mare than 2048-stick.x-100
Code edit (1 edits merged)
Please save this source code
User prompt
revert platformMinWidth calculation : wider on first levelrs, thiner on high levels
User prompt
now add a ratio to make minimum width depend on the level (larger for first levels, then more and more thin)
User prompt
now add a ratio to make width depend on the level (larger for first levels, then more and more thin)
User prompt
use them handleNinjaReachPlatform
User prompt
create global variables for platformMinWidth and platformMaxWidth
Code edit (2 edits merged)
Please save this source code
User prompt
in game.update when isReturningToBase, ensure platformNext.x don't go below platformTarget.x+200
User prompt
make the ninjaappear behind the platforms
Code edit (4 edits merged)
Please save this source code
User prompt
make the stick appear behind the platforms
User prompt
make the stick appear behind the platforms
User prompt
when the stick fall end play stickTapSound
Code edit (1 edits merged)
Please save this source code
Code edit (9 edits merged)
Please save this source code
User prompt
no, declare the variables in globale scope, and init the objects in gameinitialize
User prompt
move background and startbutton init in gameinitialize, kee the variables global
User prompt
call gameinitalize
User prompt
add a function named'gameInitialize()'
User prompt
Please fix the bug: 'InternalError: too much recursion' in or related to this line: 'gameInitialize();' Line Number: 105
User prompt
move all initialization code in a function named'gameInitialize()'
Code edit (1 edits merged)
Please save this source code
===================================================================
--- original.js
+++ change.js
@@ -23,8 +23,12 @@
LK.getSound('runSound').play();
self.runSoundPlaying = true;
}
} else {
+ if (self.runSoundPlaying) {
+ LK.getSound('runSound').stop();
+ self.runSoundPlaying = false;
+ }
self.ninjaStand.visible = true;
self.ninjaRun.visible = false;
self.runSoundPlaying = false;
}
@@ -80,11 +84,32 @@
/****
* Game Code
****/
+// Initialize game variables
+var groundOffset = 2732 + 25;
+var ninja;
+var stick;
+var platformCurrent;
+var platformTarget;
+var platformNext;
+var isStickGrowing = false;
+var isReturningToBase = false;
+var isStickFalling = false;
+var isNinjaMoving = false;
+var isGameStarted = false; // Flag to indicate the start of the game
+var legsOffset = 50;
+var score = 0;
+var foreground1;
+var foreground2;
+var midground1;
+var midground2;
+var fallSoundPlayed = false; // Flag to ensure fall sound is played only once
var background;
var startButton;
var scoreTxt;
+var basePlatformX = 300;
+var NINJA_MOVEMENT_SPEED = -5;
function gameInitialize() {
// Initialize game variables
groundOffset = 2732 + 25;
ninja = null;
@@ -133,12 +158,66 @@
scoreTxt.y = 50; // Position near the top
scoreTxt.visible = false;
game.addChild(scoreTxt);
}
-// Call gameInitialize function
-gameInitialize();
-var basePlatformX = 300;
-var NINJA_MOVEMENT_SPEED = -5;
+// Start the game
+function startGame() {
+ // Background is already added in the initial game setup
+ fallSoundPlayed = false; // Reset the flag when the game starts
+ ninja = new Ninja(); // Initialize the ninja object
+ ninja.runSoundPlaying = false; // Reset the run sound flag when the game starts
+ // 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();
+ 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);
+ // Play background music every 10 seconds
+ LK.getSound('bgMusic').play();
+ LK.setInterval(function () {
+ LK.getSound('bgMusic').play();
+ }, 10000);
+}
function handleNinjaReachPlatform() {
console.log("Start of handleNinjaReachPlatform");
console.log("platformCurrent.x:", platformCurrent.x);
console.log("platformTarget.x:", platformTarget.x);
@@ -235,8 +314,9 @@
}
function fallStick() {
stick.rotation += 0.1;
if (stick.rotation >= Math.PI / 2) {
+ LK.getSound('stickTapSound').play();
stick.rotation = Math.PI / 2;
isStickFalling = false;
isNinjaMoving = true;
}
@@ -244,27 +324,30 @@
function growStick() {
stick.length += 10;
stick.height = stick.length;
}
-// Initialize game variables
-var groundOffset = 2732 + 25;
-var ninja;
-var stick;
-var platformCurrent;
-var platformTarget;
-var platformNext;
-var isStickGrowing = false;
-var isReturningToBase = false;
-var isStickFalling = false;
-var isNinjaMoving = false;
-var isGameStarted = false; // Flag to indicate the start of the game
-var legsOffset = 50;
-var score = 0;
-var foreground1;
-var foreground2;
-var midground1;
-var midground2;
-var fallSoundPlayed = false; // Flag to ensure fall sound is played only once
+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;
+ }
+}
// Create initial platforms
function createInitialPlatforms() {
var platform1 = new Platform();
platform1.x = basePlatformX;
@@ -287,66 +370,8 @@
platform3.width = Math.floor(Math.random() * (400 - 100 + 1)) + 100; // Randomize width between 100 and 400
game.addChild(platform3);
platformNext = platform3;
}
-// Start the game
-function startGame() {
- // Background is already added in the initial game setup
- fallSoundPlayed = false; // Reset the flag when the game starts
- ninja = new Ninja(); // Initialize the ninja object
- ninja.runSoundPlaying = false; // Reset the run sound flag when the game starts
- // 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();
- 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);
- // Play background music every 10 seconds
- LK.getSound('bgMusic').play();
- LK.setInterval(function () {
- LK.getSound('bgMusic').play();
- }, 10000);
-}
// Handle touch down event
game.down = function (x, y, obj) {
if (!isGameStarted) {
startButton.visible = false;
@@ -398,26 +423,6 @@
stick.x += moveSpeed * Math.sign(distanceToMove);
}
}
};
-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;
- }
-}
\ No newline at end of file
+// Call gameInitialize function
+gameInitialize();
\ No newline at end of file