/**** * Classes ****/ // Flag to indicate the intro animation // Clouds class var Clouds = Container.expand(function () { var self = Container.call(this); var bridGraphics1 = self.attachAsset('birds1', { anchorX: 0.5, anchorY: 0.5, x: -10 - Math.random() * 512, y: 480 + Math.random() * 128 }); var bridGraphics2 = self.attachAsset('birds2', { anchorX: 0.5, anchorY: 0.5, x: -20 - Math.random() * 42, y: 480 + Math.random() * 128 }); var cloudGraphics3 = self.attachAsset('clouds2', { anchorX: 0.5, anchorY: 0.5, alpha: 0.95, y: Math.random() * 320 }); var cloudGraphics4 = self.attachAsset('clouds2', { anchorX: 0.5, anchorY: 0.5, scaleX: -1.1, scaleY: 1.1, alpha: 0.95, x: Math.random() * 2048, y: Math.random() * 320 }); var cloudGraphics = self.attachAsset('clouds1', { anchorX: 0.5, anchorY: 0.5, alpha: 0.9, x: Math.random() * 512, y: Math.random() * 480 }); var cloudGraphics2 = self.attachAsset('clouds1', { anchorX: 0.5, anchorY: 0.5, alpha: 0.9, scaleX: -1.1, scaleY: 1.1, x: 1024 + Math.random() * 1024, y: Math.random() * 480 }); self.speed1 = 0.5 + 0.5 * Math.random(); self.speed2 = 0.5 + 0.5 * Math.random(); self.speed3 = 0.1 + 0.25 * Math.random(); self.speed4 = 0.1 + 0.25 * Math.random(); self.speed5 = 0.5 + 0.1 * Math.random(); self.speed6 = 1.0 + 0.2 * Math.random(); self.update = function () { cloudGraphics.x -= self.speed1; if (cloudGraphics.x + cloudGraphics.width / 2 < 0) { cloudGraphics.x = 2048 + cloudGraphics.width / 2 + Math.random() * 1024; ; cloudGraphics.y = Math.random() * 512; } cloudGraphics2.x -= self.speed2; if (cloudGraphics2.x + cloudGraphics2.width / 2 < 0) { cloudGraphics2.x = 2048 + cloudGraphics2.width / 2 + Math.random() * 1024; cloudGraphics2.y = Math.random() * 512; } cloudGraphics3.x -= self.speed3; if (cloudGraphics3.x + cloudGraphics3.width / 2 < 0) { cloudGraphics3.x = 2048 + cloudGraphics3.width / 2; cloudGraphics3.y = cloudGraphics3.heigth + Math.random() * 320; } cloudGraphics4.x -= self.speed4; if (cloudGraphics4.x + cloudGraphics4.width / 2 < 0) { cloudGraphics4.x = 2048 + cloudGraphics4.width / 2 + Math.random() * 1024; cloudGraphics4.y = cloudGraphics4.heigth + Math.random() * 320; } bridGraphics1.x += self.speed5; if (bridGraphics1.x > 2100) { bridGraphics1.x = -10 - Math.random() * 1024; bridGraphics1.y = 10 + Math.random() * 512; } bridGraphics2.x += self.speed6; if (bridGraphics2.x > 2100) { bridGraphics2.x = -20 - Math.random() * 1024; bridGraphics2.y = 320 + +Math.random() * 320; } }; }); //<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.ninja2 = self.attachAsset('ninja2', { anchorX: 0.5, anchorY: 1.0 }); self.ninja3 = self.attachAsset('ninja3', { anchorX: 0.5, anchorY: 1.0 }); self.ninja4 = self.attachAsset('ninja4', { anchorX: 0.5, anchorY: 1.0, x: 10 }); self.ninjaRun = self.attachAsset('ninjaRun', { anchorX: 0.5, anchorY: 1.0 }); self.ninjaRun1 = self.attachAsset('ninjaRun1', { anchorX: 0.5, anchorY: 1.0 }); self.ninjaRun2 = self.attachAsset('ninjaRun2', { anchorX: 0.5, anchorY: 1.0 }); self.ninjaRun3 = self.attachAsset('ninjaRun3', { anchorX: 0.5, anchorY: 1.0 }); self.ninjaRun4 = self.attachAsset('ninjaRun4', { anchorX: 0.5, anchorY: 1.0 }); self.ninjaRun.visible = false; // Initially hide the running ninja graphic self.ninjaRun1.visible = false; // Initially hide the running ninja graphic self.ninjaRun2.visible = false; // Initially hide the running ninja graphic self.ninjaRun3.visible = false; // Initially hide the running ninja graphic self.ninjaRun4.visible = false; self.ninja2.visible = false; self.ninja3.visible = false; self.ninja4.visible = false; self.runSoundPlaying = false; self.update = function () { if (intro) { return; // Skip update logic during intro } if (isNinjaMoving && !isStickFalling && stick.length && ninja.x + ninja.width / 2 < stick.x + stick.length) { self.ninjaStand.visible = false; self.ninja2.visible = false; self.ninja3.visible = false; self.ninja4.visible = false; if (LK.ticks % 10 < 5) { self.ninjaRun.visible = false; self.ninjaRun1.visible = true; } else { self.ninjaRun.visible = true; self.ninjaRun1.visible = false; } if (!self.runSoundPlaying) { self.runSoundPlaying = true; LK.getSound('runSound').play(); log("Play sound :", isNinjaMoving, !isStickFalling, stick.length, ninja.x + ninja.width / 2, stick.x + stick.length, isReturningToBase); } } else { //log("Ninja Arrived"); self.ninjaStand.visible = true; self.ninjaRun.visible = false; self.ninjaRun1.visible = false; self.ninjaRun2.visible = false; self.ninjaRun3.visible = false; self.ninjaRun4.visible = false; if (!isNinjaMoving) { self.runSoundPlaying = false; if (LK.ticks % 70 < 10) { self.ninjaStand.visible = true; self.ninja2.visible = false; self.ninja3.visible = false; self.ninja4.visible = false; } else if (LK.ticks % 70 < 20) { self.ninjaStand.visible = false; self.ninja2.visible = true; self.ninja3.visible = false; self.ninja4.visible = false; } else if (LK.ticks % 70 < 30) { self.ninjaStand.visible = false; self.ninja2.visible = false; self.ninja3.visible = true; self.ninja4.visible = false; } else if (LK.ticks % 70 < 40) { self.ninjaStand.visible = false; self.ninja2.visible = false; self.ninja3.visible = false; self.ninja4.visible = true; } else if (LK.ticks % 70 < 50) { self.ninjaStand.visible = false; self.ninja2.visible = false; self.ninja3.visible = true; self.ninja4.visible = false; } else if (LK.ticks % 70 < 60) { self.ninjaStand.visible = false; self.ninja2.visible = true; self.ninja3.visible = false; self.ninja4.visible = false; } else { self.ninjaStand.visible = true; self.ninja2.visible = false; self.ninja3.visible = false; self.ninja4.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 }; }); var StartButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics2 = self.attachAsset('startButton', { anchorX: 0.5, anchorY: 0.5, x: 20, y: 20, tint: 0x333333, alpha: 0.75 }); var buttonGraphics = self.attachAsset('startButton', { anchorX: 0.5, anchorY: 0.5 }); }); // 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 ****/ 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.alpha = 1; 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; } function updateScore() { LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore().toString().padStart(2, '0')); // Update score text to always display two digits var grow = true; (function () { 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 = false; var clouds; var platformMinWidth = 100; var level = 1; // Initialize the level variable var platformMaxWidth = 400; 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 = 60; 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; stick = null; platformCurrent = null; platformTarget = null; platformNext = null; isStickGrowing = false; isReturningToBase = false; isStickFalling = false; isNinjaMoving = false; isGameStarted = false; // Flag to indicate the start of the game legsOffset = 50; score = 0; foreground1 = null; foreground2 = null; midground1 = null; midground2 = null; fallSoundPlayed = false; // Flag to ensure fall sound is played only once // Add background image background = LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 }); game.addChild(background); startButton = new StartButton(); startButton.x = 2048 / 2; startButton.y = 2732 / 2; game.addChild(startButton); // Create and display the score text at the top of the screen with drop shadow scoreTxt = new Text2(LK.getScore().toString().padStart(2, '0'), { size: 300, fill: "#ffffff", stroke: "#000000", strokeThickness: 10, dropShadow: true, dropShadowColor: "#000000", dropShadowBlur: 4, dropShadowDistance: 6, weight: 1000 }); scoreTxt.anchor.set(0.5, 0); // Center the score text horizontally, anchor point set at the middle of its top edge. scoreTxt.x = 2048 / 2; // Center horizontally scoreTxt.y = 50; // Position near the top scoreTxt.visible = false; scoreTxt.originalWidth = scoreTxt.width; // Store original width scoreTxt.originalHeight = scoreTxt.height; // Store original height game.addChild(scoreTxt); // Add clouds to the game clouds = new Clouds(); game.addChildAt(clouds, 1); } // 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(); ninja.ninjaStand.visible = false; ninja.ninjaRun.visible = true; ninja.x = -ninja.ninjaRun.width; // Start ninja off-screen to the left ninja.y = platformCurrent.y - platformCurrent.height + 25; // Adjusted y-coordinate to ensure ninja is visible on the first hill with an offset of 20 log("ninja.y set in startGame:", ninja.y); // Animate ninja jumping to the first platform var jumpInterval = LK.setInterval(function () { ninja.x += 20; // Adjust the speed of the jump as needed ninja.y = platformCurrent.y - platformCurrent.height + 25 - Math.sin(ninja.x / platformCurrent.x * Math.PI) * 100; // Parabolic jump if (ninja.x >= platformCurrent.x) { ninja.x = platformCurrent.x; // Snap to the platform position ninja.y = platformCurrent.y - platformCurrent.height + 25; // Ensure ninja lands on the platform LK.clearInterval(jumpInterval); // Stop the animation intro = false; // Set intro flag to false after intro animation ninja.ninjaRun.visible = false; ninja.ninjaStand.visible = true; LK.getSound('stickTapSound').play(); // Play fall sound when ninja lands on the platform ninja.ninjaRun.visible = false; ninja.ninjaStand.visible = true; } }, 16); // Approximately 60 FPS game.addChildAt(ninja, game.children.indexOf(platformCurrent) - 1); stick = new Stick(); stick.length = 0; // Set initial stick length to zero stick.width = 15; 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, 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(); }, 8730); } function handleNinjaReachPlatform() { log("Start of handleNinjaReachPlatform", isNinjaMoving); transitionPlatforms(); updateScore(); positionNextPlatform(); log("End of handleNinjaReachPlatform", isNinjaMoving); } function handleNinjaFall() { // If the stick does not reach the next platform or exceeds it, make the ninja fall ninja.y += 20; log("ninja.y set in handleNinjaFall:", ninja.y); if (ninja.runSoundPlaying) { LK.clearInterval(ninja.runSoundInterval); ninja.runSoundPlaying = false; } ninja.ninjaStand.rotation = Math.PI; // Rotate the ninja by 180 degrees if (!fallSoundPlayed) { LK.getSound('fallSound').play(); fallSoundPlayed = true; // Set the flag to true after playing the sound } if (ninja.y > 2732 && isNinjaMoving) { // If ninja falls off the screen LK.setTimeout(function () { LK.showGameOver(); }, 1000); // 1-second delay before showing game over screen } } function handleNinjaAfterStick() { 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 && isNinjaMoving) { 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; if (ninjaMaxX < platformMinX || ninjaMinX > platformMaxX) { handleNinjaFall(); } else { handleNinjaReachPlatform(); } } } function moveNinja() { //log("Moving...", "ninja.x= " + ninja.x, "stick.x= " + (stick.x + stick.length)); // Shift platforms, ninja, and stick to the left if (isNinjaMoving) { handleBackgroundMovement(NINJA_MOVEMENT_SPEED); platformCurrent.x += NINJA_MOVEMENT_SPEED; platformTarget.x += NINJA_MOVEMENT_SPEED; platformNext.x += NINJA_MOVEMENT_SPEED; ninja.x += NINJA_MOVEMENT_SPEED; stick.x += NINJA_MOVEMENT_SPEED; } // Make the ninja walk on the stick if (ninja.x < stick.x + stick.length) { ninja.x += 10; } else { handleNinjaAfterStick(); } } function fallStick() { 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() { 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 midground1.x += distance * 0.8; midground2.x += distance * 0.8; // Move foreground images to the left foreground1.x += distance * 1.5; foreground2.x += distance * 1.5; // 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; } } function handleReturningToBase() { var distanceToMove = basePlatformX - platformCurrent.x; var moveSpeed = 10; // Adjust the speed as needed if (Math.abs(distanceToMove) <= moveSpeed) { // If the distance to move is less than or equal to the move speed, snap to position platformCurrent.x = basePlatformX; //platformTarget.x += distanceToMove; //platformNext.x += distanceToMove; //ninja.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; */ resetStick(); isReturningToBase = false; // Stop moving objects } else { // Move objects towards the base position platformCurrent.x += moveSpeed * Math.sign(distanceToMove); platformTarget.x += moveSpeed * Math.sign(distanceToMove); platformNext.x += moveSpeed * Math.sign(distanceToMove) * (platformNext.speedRatio || 1); if (platformNext.x - platformNext.width < 2048) { platformNext.x = 2048 + platformNext.width; } ninja.x += moveSpeed * Math.sign(distanceToMove); stick.x += moveSpeed * Math.sign(distanceToMove); //log("ninja.y set in handleReturningToBase:", ninja.y); if (stick.alpha > 0) { stick.alpha -= 0.025; } else { stick.alpha = 0; } //log("stick.y set in handleReturningToBase:", stick.y); handleBackgroundMovement(moveSpeed * Math.sign(distanceToMove)); } } // Create initial platforms function createInitialPlatforms() { var platform1 = new Platform(); platform1.x = basePlatformX; platform1.y = groundOffset; // Adjusted y-coordinate to touch the ground log("ninja.y set in createInitialPlatforms:", ninja.y); platformMinWidth = Math.max(50, 300 - level * 10); // Decrease minimum width as level increases, with a minimum of 50 platform1.width = Math.floor(Math.random() * (platformMaxWidth - platformMinWidth + 1)) + platformMinWidth; // Randomize width between platformMinWidth and platformMaxWidth game.addChild(platform1); platformCurrent = platform1; var platform2 = new Platform(); 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; platform2.x = platform1.x + platform1.width + distance; platform2.y = groundOffset; // Adjusted y-coordinate to touch the ground platform2.width = Math.floor(Math.random() * (platformMaxWidth - platformMinWidth + 1)) + platformMinWidth; // Randomize width between platformMinWidth and platformMaxWidth game.addChild(platform2); platformTarget = platform2; var platform3 = new Platform(); platform3.x = 2048 + distance; // Position the third platform outside the screen platform3.y = groundOffset; // Adjusted y-coordinate to touch the ground platform3.width = Math.floor(Math.random() * (platformMaxWidth - platformMinWidth + 1)) + platformMinWidth; // Randomize width between platformMinWidth and platformMaxWidth game.addChild(platform3); platformNext = platform3; } function log() { if (isDebug) { var _console; (_console = console).log.apply(_console, arguments); } } // Handle touch down event game.down = function (x, y, obj) { if (!isGameStarted && startButton.alpha == 1) { var startClearInterval = LK.setInterval(function () { startButton.alpha -= 0.05; if (startButton.alpha <= 0) { LK.clearInterval(startClearInterval); } }, 40); LK.getSound('startSound').play(); LK.setTimeout(function () { startButton.visible = false; scoreTxt.visible = true; isGameStarted = true; startGame(); }, 1000); } else if (!intro && !isStickGrowing && !isStickFalling && !isNinjaMoving) { isStickGrowing = true; LK.getSound('stickGrowSound').play(); } }; // Handle touch up event game.up = function (x, y, obj) { if (isStickGrowing && !isNinjaMoving) { isStickGrowing = false; isStickFalling = true; LK.getSound('stickGrowSound').stop(); } }; // Update game logic game.update = function () { if (isStickGrowing) { growStick(); } if (ninja) { ninja.update(); } if (isStickFalling && !isNinjaMoving) { fallStick(); } if (isNinjaMoving) { moveNinja(); } if (isReturningToBase) { handleReturningToBase(); } }; // Call gameInitialize function gameInitialize();
/****
* Classes
****/
// Flag to indicate the intro animation
// Clouds class
var Clouds = Container.expand(function () {
var self = Container.call(this);
var bridGraphics1 = self.attachAsset('birds1', {
anchorX: 0.5,
anchorY: 0.5,
x: -10 - Math.random() * 512,
y: 480 + Math.random() * 128
});
var bridGraphics2 = self.attachAsset('birds2', {
anchorX: 0.5,
anchorY: 0.5,
x: -20 - Math.random() * 42,
y: 480 + Math.random() * 128
});
var cloudGraphics3 = self.attachAsset('clouds2', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.95,
y: Math.random() * 320
});
var cloudGraphics4 = self.attachAsset('clouds2', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: -1.1,
scaleY: 1.1,
alpha: 0.95,
x: Math.random() * 2048,
y: Math.random() * 320
});
var cloudGraphics = self.attachAsset('clouds1', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.9,
x: Math.random() * 512,
y: Math.random() * 480
});
var cloudGraphics2 = self.attachAsset('clouds1', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.9,
scaleX: -1.1,
scaleY: 1.1,
x: 1024 + Math.random() * 1024,
y: Math.random() * 480
});
self.speed1 = 0.5 + 0.5 * Math.random();
self.speed2 = 0.5 + 0.5 * Math.random();
self.speed3 = 0.1 + 0.25 * Math.random();
self.speed4 = 0.1 + 0.25 * Math.random();
self.speed5 = 0.5 + 0.1 * Math.random();
self.speed6 = 1.0 + 0.2 * Math.random();
self.update = function () {
cloudGraphics.x -= self.speed1;
if (cloudGraphics.x + cloudGraphics.width / 2 < 0) {
cloudGraphics.x = 2048 + cloudGraphics.width / 2 + Math.random() * 1024;
;
cloudGraphics.y = Math.random() * 512;
}
cloudGraphics2.x -= self.speed2;
if (cloudGraphics2.x + cloudGraphics2.width / 2 < 0) {
cloudGraphics2.x = 2048 + cloudGraphics2.width / 2 + Math.random() * 1024;
cloudGraphics2.y = Math.random() * 512;
}
cloudGraphics3.x -= self.speed3;
if (cloudGraphics3.x + cloudGraphics3.width / 2 < 0) {
cloudGraphics3.x = 2048 + cloudGraphics3.width / 2;
cloudGraphics3.y = cloudGraphics3.heigth + Math.random() * 320;
}
cloudGraphics4.x -= self.speed4;
if (cloudGraphics4.x + cloudGraphics4.width / 2 < 0) {
cloudGraphics4.x = 2048 + cloudGraphics4.width / 2 + Math.random() * 1024;
cloudGraphics4.y = cloudGraphics4.heigth + Math.random() * 320;
}
bridGraphics1.x += self.speed5;
if (bridGraphics1.x > 2100) {
bridGraphics1.x = -10 - Math.random() * 1024;
bridGraphics1.y = 10 + Math.random() * 512;
}
bridGraphics2.x += self.speed6;
if (bridGraphics2.x > 2100) {
bridGraphics2.x = -20 - Math.random() * 1024;
bridGraphics2.y = 320 + +Math.random() * 320;
}
};
});
//<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.ninja2 = self.attachAsset('ninja2', {
anchorX: 0.5,
anchorY: 1.0
});
self.ninja3 = self.attachAsset('ninja3', {
anchorX: 0.5,
anchorY: 1.0
});
self.ninja4 = self.attachAsset('ninja4', {
anchorX: 0.5,
anchorY: 1.0,
x: 10
});
self.ninjaRun = self.attachAsset('ninjaRun', {
anchorX: 0.5,
anchorY: 1.0
});
self.ninjaRun1 = self.attachAsset('ninjaRun1', {
anchorX: 0.5,
anchorY: 1.0
});
self.ninjaRun2 = self.attachAsset('ninjaRun2', {
anchorX: 0.5,
anchorY: 1.0
});
self.ninjaRun3 = self.attachAsset('ninjaRun3', {
anchorX: 0.5,
anchorY: 1.0
});
self.ninjaRun4 = self.attachAsset('ninjaRun4', {
anchorX: 0.5,
anchorY: 1.0
});
self.ninjaRun.visible = false; // Initially hide the running ninja graphic
self.ninjaRun1.visible = false; // Initially hide the running ninja graphic
self.ninjaRun2.visible = false; // Initially hide the running ninja graphic
self.ninjaRun3.visible = false; // Initially hide the running ninja graphic
self.ninjaRun4.visible = false;
self.ninja2.visible = false;
self.ninja3.visible = false;
self.ninja4.visible = false;
self.runSoundPlaying = false;
self.update = function () {
if (intro) {
return; // Skip update logic during intro
}
if (isNinjaMoving && !isStickFalling && stick.length && ninja.x + ninja.width / 2 < stick.x + stick.length) {
self.ninjaStand.visible = false;
self.ninja2.visible = false;
self.ninja3.visible = false;
self.ninja4.visible = false;
if (LK.ticks % 10 < 5) {
self.ninjaRun.visible = false;
self.ninjaRun1.visible = true;
} else {
self.ninjaRun.visible = true;
self.ninjaRun1.visible = false;
}
if (!self.runSoundPlaying) {
self.runSoundPlaying = true;
LK.getSound('runSound').play();
log("Play sound :", isNinjaMoving, !isStickFalling, stick.length, ninja.x + ninja.width / 2, stick.x + stick.length, isReturningToBase);
}
} else {
//log("Ninja Arrived");
self.ninjaStand.visible = true;
self.ninjaRun.visible = false;
self.ninjaRun1.visible = false;
self.ninjaRun2.visible = false;
self.ninjaRun3.visible = false;
self.ninjaRun4.visible = false;
if (!isNinjaMoving) {
self.runSoundPlaying = false;
if (LK.ticks % 70 < 10) {
self.ninjaStand.visible = true;
self.ninja2.visible = false;
self.ninja3.visible = false;
self.ninja4.visible = false;
} else if (LK.ticks % 70 < 20) {
self.ninjaStand.visible = false;
self.ninja2.visible = true;
self.ninja3.visible = false;
self.ninja4.visible = false;
} else if (LK.ticks % 70 < 30) {
self.ninjaStand.visible = false;
self.ninja2.visible = false;
self.ninja3.visible = true;
self.ninja4.visible = false;
} else if (LK.ticks % 70 < 40) {
self.ninjaStand.visible = false;
self.ninja2.visible = false;
self.ninja3.visible = false;
self.ninja4.visible = true;
} else if (LK.ticks % 70 < 50) {
self.ninjaStand.visible = false;
self.ninja2.visible = false;
self.ninja3.visible = true;
self.ninja4.visible = false;
} else if (LK.ticks % 70 < 60) {
self.ninjaStand.visible = false;
self.ninja2.visible = true;
self.ninja3.visible = false;
self.ninja4.visible = false;
} else {
self.ninjaStand.visible = true;
self.ninja2.visible = false;
self.ninja3.visible = false;
self.ninja4.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
};
});
var StartButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics2 = self.attachAsset('startButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 20,
y: 20,
tint: 0x333333,
alpha: 0.75
});
var buttonGraphics = self.attachAsset('startButton', {
anchorX: 0.5,
anchorY: 0.5
});
});
// 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
****/
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.alpha = 1;
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;
}
function updateScore() {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore().toString().padStart(2, '0')); // Update score text to always display two digits
var grow = true;
(function () {
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 = false;
var clouds;
var platformMinWidth = 100;
var level = 1; // Initialize the level variable
var platformMaxWidth = 400;
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 = 60;
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;
stick = null;
platformCurrent = null;
platformTarget = null;
platformNext = null;
isStickGrowing = false;
isReturningToBase = false;
isStickFalling = false;
isNinjaMoving = false;
isGameStarted = false; // Flag to indicate the start of the game
legsOffset = 50;
score = 0;
foreground1 = null;
foreground2 = null;
midground1 = null;
midground2 = null;
fallSoundPlayed = false; // Flag to ensure fall sound is played only once
// Add background image
background = LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(background);
startButton = new StartButton();
startButton.x = 2048 / 2;
startButton.y = 2732 / 2;
game.addChild(startButton);
// Create and display the score text at the top of the screen with drop shadow
scoreTxt = new Text2(LK.getScore().toString().padStart(2, '0'), {
size: 300,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 10,
dropShadow: true,
dropShadowColor: "#000000",
dropShadowBlur: 4,
dropShadowDistance: 6,
weight: 1000
});
scoreTxt.anchor.set(0.5, 0); // Center the score text horizontally, anchor point set at the middle of its top edge.
scoreTxt.x = 2048 / 2; // Center horizontally
scoreTxt.y = 50; // Position near the top
scoreTxt.visible = false;
scoreTxt.originalWidth = scoreTxt.width; // Store original width
scoreTxt.originalHeight = scoreTxt.height; // Store original height
game.addChild(scoreTxt);
// Add clouds to the game
clouds = new Clouds();
game.addChildAt(clouds, 1);
}
// 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();
ninja.ninjaStand.visible = false;
ninja.ninjaRun.visible = true;
ninja.x = -ninja.ninjaRun.width; // Start ninja off-screen to the left
ninja.y = platformCurrent.y - platformCurrent.height + 25; // Adjusted y-coordinate to ensure ninja is visible on the first hill with an offset of 20
log("ninja.y set in startGame:", ninja.y);
// Animate ninja jumping to the first platform
var jumpInterval = LK.setInterval(function () {
ninja.x += 20; // Adjust the speed of the jump as needed
ninja.y = platformCurrent.y - platformCurrent.height + 25 - Math.sin(ninja.x / platformCurrent.x * Math.PI) * 100; // Parabolic jump
if (ninja.x >= platformCurrent.x) {
ninja.x = platformCurrent.x; // Snap to the platform position
ninja.y = platformCurrent.y - platformCurrent.height + 25; // Ensure ninja lands on the platform
LK.clearInterval(jumpInterval); // Stop the animation
intro = false; // Set intro flag to false after intro animation
ninja.ninjaRun.visible = false;
ninja.ninjaStand.visible = true;
LK.getSound('stickTapSound').play(); // Play fall sound when ninja lands on the platform
ninja.ninjaRun.visible = false;
ninja.ninjaStand.visible = true;
}
}, 16); // Approximately 60 FPS
game.addChildAt(ninja, game.children.indexOf(platformCurrent) - 1);
stick = new Stick();
stick.length = 0; // Set initial stick length to zero
stick.width = 15;
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,
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();
}, 8730);
}
function handleNinjaReachPlatform() {
log("Start of handleNinjaReachPlatform", isNinjaMoving);
transitionPlatforms();
updateScore();
positionNextPlatform();
log("End of handleNinjaReachPlatform", isNinjaMoving);
}
function handleNinjaFall() {
// If the stick does not reach the next platform or exceeds it, make the ninja fall
ninja.y += 20;
log("ninja.y set in handleNinjaFall:", ninja.y);
if (ninja.runSoundPlaying) {
LK.clearInterval(ninja.runSoundInterval);
ninja.runSoundPlaying = false;
}
ninja.ninjaStand.rotation = Math.PI; // Rotate the ninja by 180 degrees
if (!fallSoundPlayed) {
LK.getSound('fallSound').play();
fallSoundPlayed = true; // Set the flag to true after playing the sound
}
if (ninja.y > 2732 && isNinjaMoving) {
// If ninja falls off the screen
LK.setTimeout(function () {
LK.showGameOver();
}, 1000); // 1-second delay before showing game over screen
}
}
function handleNinjaAfterStick() {
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 && isNinjaMoving) {
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;
if (ninjaMaxX < platformMinX || ninjaMinX > platformMaxX) {
handleNinjaFall();
} else {
handleNinjaReachPlatform();
}
}
}
function moveNinja() {
//log("Moving...", "ninja.x= " + ninja.x, "stick.x= " + (stick.x + stick.length));
// Shift platforms, ninja, and stick to the left
if (isNinjaMoving) {
handleBackgroundMovement(NINJA_MOVEMENT_SPEED);
platformCurrent.x += NINJA_MOVEMENT_SPEED;
platformTarget.x += NINJA_MOVEMENT_SPEED;
platformNext.x += NINJA_MOVEMENT_SPEED;
ninja.x += NINJA_MOVEMENT_SPEED;
stick.x += NINJA_MOVEMENT_SPEED;
}
// Make the ninja walk on the stick
if (ninja.x < stick.x + stick.length) {
ninja.x += 10;
} else {
handleNinjaAfterStick();
}
}
function fallStick() {
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() {
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
midground1.x += distance * 0.8;
midground2.x += distance * 0.8;
// Move foreground images to the left
foreground1.x += distance * 1.5;
foreground2.x += distance * 1.5;
// 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;
}
}
function handleReturningToBase() {
var distanceToMove = basePlatformX - platformCurrent.x;
var moveSpeed = 10; // Adjust the speed as needed
if (Math.abs(distanceToMove) <= moveSpeed) {
// If the distance to move is less than or equal to the move speed, snap to position
platformCurrent.x = basePlatformX;
//platformTarget.x += distanceToMove;
//platformNext.x += distanceToMove;
//ninja.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;
*/
resetStick();
isReturningToBase = false; // Stop moving objects
} else {
// Move objects towards the base position
platformCurrent.x += moveSpeed * Math.sign(distanceToMove);
platformTarget.x += moveSpeed * Math.sign(distanceToMove);
platformNext.x += moveSpeed * Math.sign(distanceToMove) * (platformNext.speedRatio || 1);
if (platformNext.x - platformNext.width < 2048) {
platformNext.x = 2048 + platformNext.width;
}
ninja.x += moveSpeed * Math.sign(distanceToMove);
stick.x += moveSpeed * Math.sign(distanceToMove);
//log("ninja.y set in handleReturningToBase:", ninja.y);
if (stick.alpha > 0) {
stick.alpha -= 0.025;
} else {
stick.alpha = 0;
}
//log("stick.y set in handleReturningToBase:", stick.y);
handleBackgroundMovement(moveSpeed * Math.sign(distanceToMove));
}
}
// Create initial platforms
function createInitialPlatforms() {
var platform1 = new Platform();
platform1.x = basePlatformX;
platform1.y = groundOffset; // Adjusted y-coordinate to touch the ground
log("ninja.y set in createInitialPlatforms:", ninja.y);
platformMinWidth = Math.max(50, 300 - level * 10); // Decrease minimum width as level increases, with a minimum of 50
platform1.width = Math.floor(Math.random() * (platformMaxWidth - platformMinWidth + 1)) + platformMinWidth; // Randomize width between platformMinWidth and platformMaxWidth
game.addChild(platform1);
platformCurrent = platform1;
var platform2 = new Platform();
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;
platform2.x = platform1.x + platform1.width + distance;
platform2.y = groundOffset; // Adjusted y-coordinate to touch the ground
platform2.width = Math.floor(Math.random() * (platformMaxWidth - platformMinWidth + 1)) + platformMinWidth; // Randomize width between platformMinWidth and platformMaxWidth
game.addChild(platform2);
platformTarget = platform2;
var platform3 = new Platform();
platform3.x = 2048 + distance; // Position the third platform outside the screen
platform3.y = groundOffset; // Adjusted y-coordinate to touch the ground
platform3.width = Math.floor(Math.random() * (platformMaxWidth - platformMinWidth + 1)) + platformMinWidth; // Randomize width between platformMinWidth and platformMaxWidth
game.addChild(platform3);
platformNext = platform3;
}
function log() {
if (isDebug) {
var _console;
(_console = console).log.apply(_console, arguments);
}
}
// Handle touch down event
game.down = function (x, y, obj) {
if (!isGameStarted && startButton.alpha == 1) {
var startClearInterval = LK.setInterval(function () {
startButton.alpha -= 0.05;
if (startButton.alpha <= 0) {
LK.clearInterval(startClearInterval);
}
}, 40);
LK.getSound('startSound').play();
LK.setTimeout(function () {
startButton.visible = false;
scoreTxt.visible = true;
isGameStarted = true;
startGame();
}, 1000);
} else if (!intro && !isStickGrowing && !isStickFalling && !isNinjaMoving) {
isStickGrowing = true;
LK.getSound('stickGrowSound').play();
}
};
// Handle touch up event
game.up = function (x, y, obj) {
if (isStickGrowing && !isNinjaMoving) {
isStickGrowing = false;
isStickFalling = true;
LK.getSound('stickGrowSound').stop();
}
};
// Update game logic
game.update = function () {
if (isStickGrowing) {
growStick();
}
if (ninja) {
ninja.update();
}
if (isStickFalling && !isNinjaMoving) {
fallStick();
}
if (isNinjaMoving) {
moveNinja();
}
if (isReturningToBase) {
handleReturningToBase();
}
};
// Call gameInitialize function
gameInitialize();