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
Code edit (1 edits merged)
Please save this source code
User prompt
in growStick, stick should not grow if it has falled
User prompt
update fallen properly
User prompt
add a property fallen to stick Class
User prompt
in growStick, stick should not grow if it has falled
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: stick is null' in or related to this line: 'stick.length = 0;' Line Number: 428
User prompt
maybe stick should be set to nextStick after the ninja stoped on the plaftform
User prompt
growStick should be updated to handle nextStick properly and avoid growing stick if it has already falled
User prompt
the nextStick should be prepared for platformTarget not current
User prompt
`(stick.length >= platformTarget.x - platformCurrent.x) ` seems missing stick.x
User prompt
fix : the comment says "// Check if current stick reached the platform" but the code check ninja.x
===================================================================
--- original.js
+++ change.js
@@ -203,8 +203,61 @@
/****
* Game Code
****/
+function positionNextPlatform() {
+ var minDistance = Math.max(100, 300 - level * 10); // Decrease minimum distance as level increases, with a minimum of 100
+ var maxDistance = Math.min(600, 400 + level * 20); // Increase maximum distance as level increases, with a maximum of 600
+ var distance = (Math.floor(Math.random() * (maxDistance - minDistance + 1)) + minDistance) * platformNext.speedRatio;
+ platformNext.x = 2048 + distance; //platformTarget.x + platformTarget.width / 2 + distance;
+ platformNext.y = groundOffset; // Adjusted y-coordinate to touch the ground
+ log("ninja.y set in handleNinjaReachPlatform:", ninja.y);
+ platformMinWidth = Math.max(50, 300 - level * 10); // Decrease minimum width as level increases, with a minimum of 50
+ platformNext.width = Math.floor(Math.random() * (platformMaxWidth - platformMinWidth + 1)) + platformMinWidth; // Randomize width between platformMinWidth and platformMaxWidth
+ level++; // Increment the level after each platform is reached
+ platformNext.visible = true;
+ platformNext.speedRatio = 1 + Math.random();
+}
+function resetStick() {
+ 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;
+ // Remove the first platform from the game and the platforms array
+ platformCurrent.visible = false;
+ var tempPlatform = platformCurrent;
+ platformCurrent = platformTarget;
+ platformTarget = platformNext;
+ platformNext = tempPlatform;
+ LK.setScore(LK.getScore() + 1);
+ scoreTxt.setText(LK.getScore()); // Update score text
+}
+function animateScoreBump() {
+ var grow = true;
+ var bumpInterval = LK.setInterval(function () {
+ if (grow) {
+ scoreTxt.width += scoreTxt.originalWidth * 0.05;
+ scoreTxt.height += scoreTxt.originalHeight * 0.05;
+ if (scoreTxt.width >= scoreTxt.originalWidth * 1.5 && scoreTxt.height >= scoreTxt.originalHeight * 1.5) {
+ grow = false;
+ }
+ } else {
+ scoreTxt.width -= scoreTxt.originalWidth * 0.05;
+ scoreTxt.height -= scoreTxt.originalHeight * 0.05;
+ if (scoreTxt.width <= scoreTxt.originalWidth && scoreTxt.height <= scoreTxt.originalHeight) {
+ scoreTxt.width = scoreTxt.originalWidth; // Reset to original width
+ scoreTxt.height = scoreTxt.originalHeight; // Reset to original height
+ LK.clearInterval(bumpInterval);
+ }
+ }
+ }, 16); // Approximately 60 FPS
+}
// Initialize game variables
var intro = true;
var isDebug = true;
var clouds;
@@ -367,67 +420,12 @@
log("Start of handleNinjaReachPlatform", isNinjaMoving);
log("platformCurrent.x:", platformCurrent.x);
log("platformTarget.x:", platformTarget.x);
log("platformNext.x:", platformNext.x);
- //isNinjaMoving = false;
- isReturningToBase = true;
- // Move ninja to the center of the platform
- var targetX = platformCurrent.x;
- var targetWidth = platformCurrent.width;
- var ninjaCenterX = targetX - targetWidth / 2 + ninja.width / 2;
- var moveNinjaToCenter = LK.setInterval(function () {
- if (ninja.x < ninjaCenterX) {
- ninja.x += 5; // Adjust the speed as needed
- } else {
- ninja.x = ninjaCenterX; // Snap to the center position
- isReturningToBase = true;
- LK.clearInterval(moveNinjaToCenter); // Stop the animation
- }
- }, 16); // Approximately 60 FPS
- // Remove the first platform from the game and the platforms array
- platformCurrent.visible = false;
- var tempPlatform = platformCurrent;
- platformCurrent = platformTarget;
- platformTarget = platformNext;
- platformNext = tempPlatform;
- LK.setScore(LK.getScore() + 1);
- scoreTxt.setText(LK.getScore()); // Update score text
- // Animate score bump
- var grow = true;
- var bumpInterval = LK.setInterval(function () {
- if (grow) {
- scoreTxt.width += scoreTxt.originalWidth * 0.05;
- scoreTxt.height += scoreTxt.originalHeight * 0.05;
- if (scoreTxt.width >= scoreTxt.originalWidth * 1.5 && scoreTxt.height >= scoreTxt.originalHeight * 1.5) {
- grow = false;
- }
- } else {
- scoreTxt.width -= scoreTxt.originalWidth * 0.05;
- scoreTxt.height -= scoreTxt.originalHeight * 0.05;
- if (scoreTxt.width <= scoreTxt.originalWidth && scoreTxt.height <= scoreTxt.originalHeight) {
- scoreTxt.width = scoreTxt.originalWidth; // Reset to original width
- scoreTxt.height = scoreTxt.originalHeight; // Reset to original height
- LK.clearInterval(bumpInterval);
- }
- }
- }, 16); // Approximately 60 FPS
- 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);
- var minDistance = Math.max(100, 300 - level * 10); // Decrease minimum distance as level increases, with a minimum of 100
- var maxDistance = Math.min(600, 400 + level * 20); // Increase maximum distance as level increases, with a maximum of 600
- var distance = (Math.floor(Math.random() * (maxDistance - minDistance + 1)) + minDistance) * platformNext.speedRatio;
- platformNext.x = 2048 + distance; //platformTarget.x + platformTarget.width / 2 + distance;
- platformNext.y = groundOffset; // Adjusted y-coordinate to touch the ground
- log("ninja.y set in handleNinjaReachPlatform:", ninja.y);
- platformMinWidth = Math.max(50, 300 - level * 10); // Decrease minimum width as level increases, with a minimum of 50
- platformNext.width = Math.floor(Math.random() * (platformMaxWidth - platformMinWidth + 1)) + platformMinWidth; // Randomize width between platformMinWidth and platformMaxWidth
- level++; // Increment the level after each platform is reached
- platformNext.visible = true;
- platformNext.speedRatio = 1 + Math.random();
+ transitionPlatforms();
+ animateScoreBump();
+ resetStick();
+ positionNextPlatform();
log("End of handleNinjaReachPlatform", isNinjaMoving);
log("platformCurrent.x:", platformCurrent.x);
log("platformTarget.x:", platformTarget.x);
log("platformNext.x:", platformNext.x);