Code edit (1 edits merged)
Please save this source code
User prompt
while reternuing to base the stick should be shorten progressively to 0 insteaf of disappearing
User prompt
insteaf of disappearing when reternuing to base the stick should be shorten progressively to 0
User prompt
insteaf of disappearing when reternuing to base the stick should be shorten to 0
Code edit (1 edits merged)
Please save this source code
User prompt
the first colum disapears (is moved) too early, it shoud pass totaly the left of the screen before moving
Code edit (4 edits merged)
Please save this source code
User prompt
in bumpInterval fix the scope problem
Code edit (1 edits merged)
Please save this source code
User prompt
fix game : can't play because stick start to grow then disapears
User prompt
fix growStick
User prompt
be coherent, currentStick is sometimes local and others global
User prompt
Please fix the bug: 'TypeError: currentStick is undefined' in or related to this line: 'if (!isNinjaMoving && currentStick.rotation >= Math.PI / 2) {' Line Number: 519
User prompt
continue the rework as the ninja doesn't move on the stick
User prompt
continue the rework as there global variable stick is still used
User prompt
Please fix the bug: 'TypeError: currentStick is undefined' in or related to this line: 'if (currentStick.length < 2048 - currentStick.x - 100) {' Line Number: 521
User prompt
change the logic of sticks, be creating each time a new stick and destroying them when their x < length
Code edit (1 edits merged)
Please save this source code
User prompt
rename animateScoreBump to updateScore and move score increase instruction in it
User prompt
refactor handleNinjaReachPlatform to 1 responsibility functions
Code edit (3 edits merged)
Please save this source code
User prompt
when ninja reach platform, move it slowly to the center of the platform
Code edit (1 edits merged)
Please save this source code
User prompt
nextstick should be at the same z-index as stick
User prompt
nextStick should be created at the same time as stick but kept invisible until needed
===================================================================
--- original.js
+++ change.js
@@ -217,17 +217,14 @@
platformNext.visible = true;
platformNext.speedRatio = 1 + Math.random();
}
function resetStick() {
- if (sticks.length > 0) {
- var currentStick = sticks[sticks.length - 1];
- currentStick.length = 0;
- currentStick.height = 0;
- currentStick.rotation = 0;
- currentStick.x = platformCurrent.x + platformCurrent.width / 2 - 30; // Set stick to the right edge of the current platform with an offset of -30
- currentStick.y = platformCurrent.y - platformCurrent.height + 30; // Adjusted y-coordinate to ensure stick is on the platform with an offset of 20
- log("stick.y set in handleNinjaReachPlatform:", currentStick.y, isNinjaMoving);
- }
+ 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 + 30; // Adjusted y-coordinate to ensure stick is on the platform with an offset of 20
+ log("stick.y set in handleNinjaReachPlatform:", stick.y, isNinjaMoving);
}
function transitionPlatforms() {
isNinjaMoving = false;
isReturningToBase = true;
@@ -268,9 +265,9 @@
var level = 1; // Initialize the level variable
var platformMaxWidth = 400;
var groundOffset = 2732 + 25;
var ninja;
-var sticks = [];
+var stick;
var platformCurrent;
var platformTarget;
var platformNext;
var isStickGrowing = false;
@@ -293,9 +290,9 @@
function gameInitialize() {
// Initialize game variables
groundOffset = 2732 + 25;
ninja = null;
- sticks = [];
+ stick = null;
platformCurrent = null;
platformTarget = null;
platformNext = null;
isStickGrowing = false;
@@ -389,17 +386,16 @@
ninja.ninjaStand.visible = true;
}
}, 16); // Approximately 60 FPS
game.addChildAt(ninja, game.children.indexOf(platformCurrent) - 1);
- var newStick = new Stick();
- newStick.length = 0; // Set initial stick length to zero
- newStick.height = 10; // Initial height of the stick
- newStick.scaleY = newStick.length / 10; // Ensure the stick grows correctly
- newStick.x = platformCurrent.x + platformCurrent.width / 2 - 30; // Set stick to the right edge of the platform with an offset of -30
- newStick.y = platformCurrent.y - platformCurrent.height + 30; // Adjusted y-coordinate to ensure stick is on the platform with an offset of 20
- log("stick.y set in startGame:", newStick.y);
- game.addChildAt(newStick, game.children.indexOf(platformCurrent) - 1);
- sticks.push(newStick);
+ stick = new Stick();
+ stick.length = 0; // Set initial stick length to zero
+ stick.width = 20;
+ 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 + 30; // Adjusted y-coordinate to ensure stick is on the platform with an offset of 20
+ log("stick.y set in startGame:", stick.y);
+ game.addChildAt(stick, game.children.indexOf(platformCurrent) - 1);
// Add foreground images to create an infinite horizontal foreground
foreground1 = LK.getAsset('foreground', {
anchorX: 0.5,
anchorY: 1.0,
@@ -472,44 +468,33 @@
platformCurrent.x += NINJA_MOVEMENT_SPEED;
platformTarget.x += NINJA_MOVEMENT_SPEED;
platformNext.x += NINJA_MOVEMENT_SPEED;
ninja.x += NINJA_MOVEMENT_SPEED;
- var currentStick = sticks[sticks.length - 1];
- if (currentStick) {
- currentStick.x += NINJA_MOVEMENT_SPEED;
- }
+ stick.x += NINJA_MOVEMENT_SPEED;
}
// Make the ninja walk on the stick
- var currentStick = sticks[sticks.length - 1];
- if (currentStick && ninja.x + ninja.width / 2 < currentStick.x + currentStick.length) {
- ninja.x += 20;
+ if (ninja.x < stick.x + stick.length) {
+ ninja.x += 10;
} else {
handleNinjaAfterStick();
}
}
function fallStick() {
- var currentStick = sticks[sticks.length - 1];
- if (currentStick) {
- currentStick.rotation += 0.1;
- if (!isNinjaMoving && currentStick.rotation >= Math.PI / 2) {
- isStickFalling = false;
- isNinjaMoving = true;
- log("Stick finished falling");
- LK.getSound('stickTapSound').play();
- currentStick.rotation = Math.PI / 2;
- }
+ stick.rotation += 0.1;
+ if (!isNinjaMoving && stick.rotation >= Math.PI / 2) {
+ isStickFalling = false;
+ isNinjaMoving = true;
+ log("Stick finnished falling");
+ LK.getSound('stickTapSound').play();
+ stick.rotation = Math.PI / 2;
}
}
function growStick() {
- var currentStick = sticks[sticks.length - 1];
- if (currentStick) {
- if (currentStick && currentStick.length < 2048 - currentStick.x - 100) {
- currentStick.length += 10;
- currentStick.height = currentStick.length;
- currentStick.scaleY = currentStick.length / 10; // Ensure the stick grows correctly
- } else {
- LK.getSound('stickGrowSound').stop();
- }
+ if (stick.length < 2048 - stick.x - 100) {
+ stick.length += 10;
+ stick.height = stick.length;
+ } else {
+ LK.getSound('stickGrowSound').stop();
}
}
function handleBackgroundMovement(distance) {
// Move midground images to the left
@@ -541,14 +526,9 @@
platformCurrent.x = basePlatformX;
platformTarget.x += distanceToMove;
//platformNext.x += distanceToMove;
ninja.x += distanceToMove;
- if (sticks.length > 0) {
- var currentStick = sticks[sticks.length - 1];
- if (currentStick) {
- currentStick.x += distanceToMove;
- }
- }
+ stick.x += distanceToMove;
foreground1.x += distanceToMove * 1.5;
foreground2.x += distanceToMove * 1.5;
midground1.x += distanceToMove * 0.5;
midground2.x += distanceToMove * 0.5;
@@ -562,12 +542,9 @@
platformNext.x = 2048 + platformNext.width;
}
ninja.x += moveSpeed * Math.sign(distanceToMove);
//log("ninja.y set in handleReturningToBase:", ninja.y);
- if (sticks.length > 0) {
- var currentStick = sticks[sticks.length - 1];
- currentStick.x += moveSpeed * Math.sign(distanceToMove);
- }
+ stick.x += moveSpeed * Math.sign(distanceToMove);
//log("stick.y set in handleReturningToBase:", stick.y);
handleBackgroundMovement(moveSpeed * Math.sign(distanceToMove));
}
}
@@ -648,13 +625,7 @@
}
if (isReturningToBase) {
handleReturningToBase();
}
- for (var i = sticks.length - 1; i >= 0; i--) {
- if (sticks[i] && sticks[i].x < sticks[i].length) {
- sticks[i].destroy();
- sticks.splice(i, 1);
- }
- }
};
// Call gameInitialize function
gameInitialize();
\ No newline at end of file