Code edit (8 edits merged)
Please save this source code
User prompt
Fix Bug: 'TypeError: Cannot set properties of undefined (setting 'x')' in or related to this line: 'grounds.gr.x = 2732;' Line Number: 138
Code edit (10 edits merged)
Please save this source code
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'move')' in or related to this line: 'grounds[i].move();' Line Number: 136
User prompt
Fix Bug: 'Uncaught ReferenceError: i is not defined' in or related to this line: 'grounds[i] = new Ground();' Line Number: 127
Code edit (1 edits merged)
Please save this source code
/**** * Classes ****/ // Grizzly Bear class var GrizzlyBear = Container.expand(function () { var self = Container.call(this); var bearGraphics = LK.getAsset('grizzlyBear', { anchorX: 0.5, anchorY: 1 }); self.addChild(bearGraphics); self.isJumping = false; self.jumpSpeed = 0; self.gravity = 0.4; self.jump = function () { if (!self.isJumping) { self.isJumping = true; self.jumpSpeed = -18; } }; self.fall = function () { if (self.isJumping) { self.jumpSpeed += self.gravity; self.y += self.jumpSpeed; if (self.y >= 2832 - self.height) { self.y = 2832 - self.height; self.isJumping = false; } } }; }); // Tree class var Tree = Container.expand(function () { var self = Container.call(this); var treeGraphics = LK.getAsset('tree', { anchorX: 0.5, anchorY: 1 }); self.addChild(treeGraphics); self.speed = 1; self.move = function () { self.x -= self.speed; if (self.x < -self.width) { self.destroy(); } }; }); // Lemming class var Lemming = Container.expand(function () { var self = Container.call(this); var lemmingGraphics = LK.getAsset('lemming', { anchorX: 0.5, anchorY: 1 }); self.addChild(lemmingGraphics); self.speed = 3; self.move = function () { self.x -= self.speed; if (self.x < -self.width) { self.destroy(); } }; }); // Rock class var Rock = Container.expand(function () { var self = Container.call(this); var rockGraphics = LK.getAsset('rock', { anchorX: 0.5, anchorY: 1 }); self.addChild(rockGraphics); self.speed = 5; self.move = function () { self.x -= self.speed; if (self.x < -self.width) { self.destroy(); } }; }); // Ground class var Ground = Container.expand(function () { var self = Container.call(this); var groundGraphics = LK.getAsset('ground', { anchorX: 0.5, anchorY: 1 }); self.addChild(groundGraphics); self.speed = 1; self.move = function () { self.x -= self.speed; if (self.x < -2048) { // Assuming the ground asset is the same width as the game width self.x = 0; // Loop the ground to create a continuous moving effect } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Initialize important asset arrays and score var lemmings = []; var trees = []; var score = 0; var rocks = []; var grounds = []; var grizzlyBear; // Create the Grizzly Bear grizzlyBear = game.addChild(new GrizzlyBear()); grizzlyBear.x = 200; grizzlyBear.y = 2732 - grizzlyBear.height + 100; // Create score display var scoreTxt = new Text2(score.toString(), { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); for (var gr = 0; gr < 5; gr++) { grounds[gr] = new Ground(); grounds[gr].x = gr * grounds[gr].width; grounds[gr].y = 2950 - grounds[gr].height; game.addChildAt(grounds[gr], 0); } // Game tick event LK.on('tick', function () { // Move the ground for (var gr = 0; gr < grounds.length; gr++) { grounds[gr].move(); } // Spawn Trees at intervals if (LK.ticks % 800 === 0) { var newTree = new Tree(); newTree.x = 2048; newTree.y = 2732 - newTree.height + 300; trees.push(newTree); game.addChildAt(newTree, 0); } // Make the Grizzly Bear fall grizzlyBear.fall(); // Move each Lemming and check for collision with GrizzlyBear for (var i = lemmings.length - 1; i >= 0; i--) { lemmings[i].move(); // Check if Lemming collides with GrizzlyBear and remove it if (grizzlyBear.intersects(lemmings[i])) { // Increase score by 10 when Grizzly Bear catches a Lemming score += 10; lemmings[i].destroy(); lemmings.splice(i, 1); continue; } // Check if Lemming is off screen and destroy it if (lemmings[i].x < -lemmings[i].width) { lemmings[i].destroy(); lemmings.splice(i, 1); } } // Move each Rock for (var i = rocks.length - 1; i >= 0; i--) { rocks[i].move(); // Check if Rock collides with GrizzlyBear and end the game if (grizzlyBear.intersects(rocks[i])) { LK.showGameOver(); } // Check if Rock is off screen and destroy it if (rocks[i].x < -rocks[i].width) { rocks[i].destroy(); rocks.splice(i, 1); } } // Move each Tree for (var i = trees.length - 1; i >= 0; i--) { trees[i].move(); // Check if Tree is off screen and destroy it if (trees[i].x < -trees[i].width) { trees[i].destroy(); trees.splice(i, 1); } } // Update score display text scoreTxt.setText(score.toString()); // Check if score reaches 100 and end the game as a win if (score >= 200) { LK.showGameOver(); } // Spawn Rocks at random intervals var rockSpawnInterval = Math.floor(Math.random() * 800) + 400; if (LK.ticks % rockSpawnInterval === 0) { var newRock = new Rock(); newRock.x = 2048; newRock.y = 2732 - newRock.height; rocks.push(newRock); game.addChildAt(newRock, game.children.length); } // Spawn Lemmings at random intervals var lemmingSpawnInterval = Math.floor(Math.random() * 200) + 40; if (LK.ticks % lemmingSpawnInterval === 0) { var newLemming = new Lemming(); newLemming.x = 2048; newLemming.y = 2732 - newLemming.height; lemmings.push(newLemming); game.addChildAt(newLemming, game.children.length); } }); // Touch event for Grizzly Bear to jump game.on('down', function () { grizzlyBear.jump(); });
===================================================================
--- original.js
+++ change.js
@@ -131,10 +131,10 @@
}
// Game tick event
LK.on('tick', function () {
// Move the ground
- for (var gr = 0; gr < 5; gr++) {
- grounds[i].move();
+ for (var gr = 0; gr < grounds.length; gr++) {
+ grounds[gr].move();
}
// Spawn Trees at intervals
if (LK.ticks % 800 === 0) {
var newTree = new Tree();
Cartoon style cute apple tree with small branches. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Cartoon style cute rock. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A cute lemming running while holding a nutella cup. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. Cartoon style.