User prompt
let the player step on the other platforms like before to collect coins in it, it stuck on their edges. and the movement when climbing on ladders is slow so add some speed for that.
User prompt
remove diagonal movement let only vertical or horizontal when holding mouse button.
User prompt
add vertical movment for the player it can't go up to the ladder!
User prompt
let the player climb the ladder when reach it!
User prompt
fix the player after finishing distance of ladder it outs and continue moving till out from the screen! it must step on the terrains when finishing climbing the ladder not to continue going up!.
User prompt
Limit the distance of player movement to be in the terrains or in ladder only so it will not out of it or from the screen, the enemies must not change their area from terrain to another, let 4 enemies only each one in its area and they can't teleport to another terrain, the enemies can move horizontal and add vertical movement tow.
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'toGlobal')' in or related to this line: 'var gamePos = game.toLocal(obj.parent.toGlobal(obj.position));' Line Number: 253
Code edit (1 edits merged)
Please save this source code
User prompt
Platform Climber
Initial prompt
Create a 2D game have 5 terrains platform like 2x2 large by 500x200px and one in the middle of the 4 terrains, add ladders to reach each area by climbing ladders, add coin at the middle of each terrain from above, add player, add 1 enemy in level1, add 'background1' for 'level1'.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Coin = Container.expand(function () { var self = Container.call(this); var coinGraphics = self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); self.collected = false; // Gentle floating animation self.floatOffset = 0; self.originalY = 0; self.update = function () { if (!self.collected) { self.floatOffset += 0.1; self.y = self.originalY + Math.sin(self.floatOffset) * 5; } }; return self; }); var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 1 }); self.speed = 1.5; self.direction = 1; self.platformBounds = { left: 0, right: 500 }; self.update = function () { self.x += self.speed * self.direction; // Bounce off platform edges if (self.x <= self.platformBounds.left || self.x >= self.platformBounds.right) { self.direction *= -1; } }; return self; }); var Ladder = Container.expand(function () { var self = Container.call(this); var ladderGraphics = self.attachAsset('ladder', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var Platform = Container.expand(function () { var self = Container.call(this); var platformGraphics = self.attachAsset('platform', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 1 }); self.speed = 4; self.climbSpeed = 3; self.isClimbing = false; self.currentPlatform = null; self.targetY = 0; self.update = function () { // Handle climbing movement if (self.isClimbing) { if (Math.abs(self.y - self.targetY) > 2) { if (self.y > self.targetY) { self.y -= self.climbSpeed; } else { self.y += self.climbSpeed; } } else { self.y = self.targetY; self.isClimbing = false; } } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ // Game state variables var currentLevel = 1; var coinsCollected = 0; var totalCoins = 5; var platforms = []; var ladders = []; var coins = []; var enemies = []; var player; // Platform positions (central hub + 4 outer platforms in 2x2 grid) var platformPositions = [{ x: 1024, y: 1366 }, // Center platform { x: 524, y: 1066 }, // Top-left { x: 1524, y: 1066 }, // Top-right { x: 524, y: 1666 }, // Bottom-left { x: 1524, y: 1666 } // Bottom-right ]; // Add background var background = game.attachAsset('background1', { anchorX: 0, anchorY: 0, x: 0, y: 0 }); // Create platforms for (var i = 0; i < platformPositions.length; i++) { var platform = new Platform(); platform.x = platformPositions[i].x; platform.y = platformPositions[i].y; platforms.push(platform); game.addChild(platform); } // Create ladders connecting platforms var ladderConnections = [{ from: 0, to: 1 }, // Center to top-left { from: 0, to: 2 }, // Center to top-right { from: 0, to: 3 }, // Center to bottom-left { from: 0, to: 4 } // Center to bottom-right ]; for (var i = 0; i < ladderConnections.length; i++) { var connection = ladderConnections[i]; var fromPlatform = platforms[connection.from]; var toPlatform = platforms[connection.to]; var ladder = new Ladder(); ladder.x = (fromPlatform.x + toPlatform.x) / 2; ladder.y = (fromPlatform.y + toPlatform.y) / 2; ladders.push(ladder); game.addChild(ladder); } // Create coins on each platform for (var i = 0; i < platforms.length; i++) { var coin = new Coin(); coin.x = platforms[i].x; coin.y = platforms[i].y - 50; coin.originalY = coin.y; coins.push(coin); game.addChild(coin); } // Create player player = new Player(); player.x = platforms[0].x; // Start on center platform player.y = platforms[0].y; player.currentPlatform = 0; game.addChild(player); // Create enemies based on level function spawnEnemies() { // Clear existing enemies for (var i = 0; i < enemies.length; i++) { enemies[i].destroy(); } enemies = []; // Spawn enemies on random platforms (excluding center) for (var i = 0; i < currentLevel; i++) { var enemyPlatformIndex = 1 + Math.floor(Math.random() * 4); // Random outer platform var enemy = new Enemy(); enemy.x = platforms[enemyPlatformIndex].x; enemy.y = platforms[enemyPlatformIndex].y; enemy.platformBounds.left = platforms[enemyPlatformIndex].x - 250; enemy.platformBounds.right = platforms[enemyPlatformIndex].x + 250; enemies.push(enemy); game.addChild(enemy); } } // Initial enemy spawn spawnEnemies(); // Score display var scoreText = new Text2('Coins: 0/' + totalCoins, { size: 60, fill: 0xFFFFFF }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); // Level display var levelText = new Text2('Level: ' + currentLevel, { size: 50, fill: 0xFFFFFF }); levelText.anchor.set(1, 0); LK.gui.topRight.addChild(levelText); // Movement controls var moveLeft = false; var moveRight = false; var moveUp = false; var moveDown = false; // Touch controls game.down = function (x, y, obj) { var gamePos = game.toLocal(obj.parent.toGlobal(obj.position)); // Determine movement direction based on touch position relative to player if (gamePos.x < player.x - 50) { moveLeft = true; } else if (gamePos.x > player.x + 50) { moveRight = true; } // Check for ladder climbing for (var i = 0; i < ladders.length; i++) { var ladder = ladders[i]; if (Math.abs(gamePos.x - ladder.x) < 80 && Math.abs(gamePos.y - ladder.y) < 100) { // Determine climb direction if (gamePos.y < player.y) { moveUp = true; } else { moveDown = true; } break; } } }; game.up = function (x, y, obj) { moveLeft = false; moveRight = false; moveUp = false; moveDown = false; }; // Game update loop game.update = function () { // Handle player movement if (!player.isClimbing) { if (moveLeft) { player.x -= player.speed; } if (moveRight) { player.x += player.speed; } // Keep player on current platform if (player.currentPlatform !== null) { var currentPlatform = platforms[player.currentPlatform]; player.x = Math.max(currentPlatform.x - 250, Math.min(currentPlatform.x + 250, player.x)); } // Check for ladder climbing if (moveUp || moveDown) { for (var i = 0; i < ladders.length; i++) { var ladder = ladders[i]; if (Math.abs(player.x - ladder.x) < 40) { player.isClimbing = true; player.x = ladder.x; // Snap to ladder // Find target platform var targetPlatformIndex = -1; if (moveUp) { // Find platform above for (var j = 0; j < platforms.length; j++) { if (platforms[j].y < player.y && Math.abs(platforms[j].x - ladder.x) < 100) { targetPlatformIndex = j; break; } } } else if (moveDown) { // Find platform below for (var j = 0; j < platforms.length; j++) { if (platforms[j].y > player.y && Math.abs(platforms[j].x - ladder.x) < 100) { targetPlatformIndex = j; break; } } } if (targetPlatformIndex !== -1) { player.targetY = platforms[targetPlatformIndex].y; player.currentPlatform = targetPlatformIndex; } break; } } } } // Check coin collection for (var i = 0; i < coins.length; i++) { var coin = coins[i]; if (!coin.collected && player.intersects(coin)) { coin.collected = true; coin.alpha = 0; coinsCollected++; LK.setScore(coinsCollected); scoreText.setText('Coins: ' + coinsCollected + '/' + totalCoins); LK.getSound('coinCollect').play(); // Check win condition if (coinsCollected >= totalCoins) { LK.showYouWin(); } } } // Check enemy collisions for (var i = 0; i < enemies.length; i++) { var enemy = enemies[i]; if (player.intersects(enemy)) { LK.getSound('enemyHit').play(); LK.effects.flashScreen(0xff0000, 500); LK.showGameOver(); } } // Level progression (spawn more enemies every 30 seconds) if (LK.ticks % (60 * 30) === 0 && coinsCollected < totalCoins) { currentLevel++; levelText.setText('Level: ' + currentLevel); spawnEnemies(); } };
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,349 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Coin = Container.expand(function () {
+ var self = Container.call(this);
+ var coinGraphics = self.attachAsset('coin', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.collected = false;
+ // Gentle floating animation
+ self.floatOffset = 0;
+ self.originalY = 0;
+ self.update = function () {
+ if (!self.collected) {
+ self.floatOffset += 0.1;
+ self.y = self.originalY + Math.sin(self.floatOffset) * 5;
+ }
+ };
+ return self;
+});
+var Enemy = Container.expand(function () {
+ var self = Container.call(this);
+ var enemyGraphics = self.attachAsset('enemy', {
+ anchorX: 0.5,
+ anchorY: 1
+ });
+ self.speed = 1.5;
+ self.direction = 1;
+ self.platformBounds = {
+ left: 0,
+ right: 500
+ };
+ self.update = function () {
+ self.x += self.speed * self.direction;
+ // Bounce off platform edges
+ if (self.x <= self.platformBounds.left || self.x >= self.platformBounds.right) {
+ self.direction *= -1;
+ }
+ };
+ return self;
+});
+var Ladder = Container.expand(function () {
+ var self = Container.call(this);
+ var ladderGraphics = self.attachAsset('ladder', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ return self;
+});
+var Platform = Container.expand(function () {
+ var self = Container.call(this);
+ var platformGraphics = self.attachAsset('platform', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ return self;
+});
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ var playerGraphics = self.attachAsset('player', {
+ anchorX: 0.5,
+ anchorY: 1
+ });
+ self.speed = 4;
+ self.climbSpeed = 3;
+ self.isClimbing = false;
+ self.currentPlatform = null;
+ self.targetY = 0;
+ self.update = function () {
+ // Handle climbing movement
+ if (self.isClimbing) {
+ if (Math.abs(self.y - self.targetY) > 2) {
+ if (self.y > self.targetY) {
+ self.y -= self.climbSpeed;
+ } else {
+ self.y += self.climbSpeed;
+ }
+ } else {
+ self.y = self.targetY;
+ self.isClimbing = false;
+ }
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+// Game state variables
+var currentLevel = 1;
+var coinsCollected = 0;
+var totalCoins = 5;
+var platforms = [];
+var ladders = [];
+var coins = [];
+var enemies = [];
+var player;
+// Platform positions (central hub + 4 outer platforms in 2x2 grid)
+var platformPositions = [{
+ x: 1024,
+ y: 1366
+},
+// Center platform
+{
+ x: 524,
+ y: 1066
+},
+// Top-left
+{
+ x: 1524,
+ y: 1066
+},
+// Top-right
+{
+ x: 524,
+ y: 1666
+},
+// Bottom-left
+{
+ x: 1524,
+ y: 1666
+} // Bottom-right
+];
+// Add background
+var background = game.attachAsset('background1', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 0,
+ y: 0
+});
+// Create platforms
+for (var i = 0; i < platformPositions.length; i++) {
+ var platform = new Platform();
+ platform.x = platformPositions[i].x;
+ platform.y = platformPositions[i].y;
+ platforms.push(platform);
+ game.addChild(platform);
+}
+// Create ladders connecting platforms
+var ladderConnections = [{
+ from: 0,
+ to: 1
+},
+// Center to top-left
+{
+ from: 0,
+ to: 2
+},
+// Center to top-right
+{
+ from: 0,
+ to: 3
+},
+// Center to bottom-left
+{
+ from: 0,
+ to: 4
+} // Center to bottom-right
+];
+for (var i = 0; i < ladderConnections.length; i++) {
+ var connection = ladderConnections[i];
+ var fromPlatform = platforms[connection.from];
+ var toPlatform = platforms[connection.to];
+ var ladder = new Ladder();
+ ladder.x = (fromPlatform.x + toPlatform.x) / 2;
+ ladder.y = (fromPlatform.y + toPlatform.y) / 2;
+ ladders.push(ladder);
+ game.addChild(ladder);
+}
+// Create coins on each platform
+for (var i = 0; i < platforms.length; i++) {
+ var coin = new Coin();
+ coin.x = platforms[i].x;
+ coin.y = platforms[i].y - 50;
+ coin.originalY = coin.y;
+ coins.push(coin);
+ game.addChild(coin);
+}
+// Create player
+player = new Player();
+player.x = platforms[0].x; // Start on center platform
+player.y = platforms[0].y;
+player.currentPlatform = 0;
+game.addChild(player);
+// Create enemies based on level
+function spawnEnemies() {
+ // Clear existing enemies
+ for (var i = 0; i < enemies.length; i++) {
+ enemies[i].destroy();
+ }
+ enemies = [];
+ // Spawn enemies on random platforms (excluding center)
+ for (var i = 0; i < currentLevel; i++) {
+ var enemyPlatformIndex = 1 + Math.floor(Math.random() * 4); // Random outer platform
+ var enemy = new Enemy();
+ enemy.x = platforms[enemyPlatformIndex].x;
+ enemy.y = platforms[enemyPlatformIndex].y;
+ enemy.platformBounds.left = platforms[enemyPlatformIndex].x - 250;
+ enemy.platformBounds.right = platforms[enemyPlatformIndex].x + 250;
+ enemies.push(enemy);
+ game.addChild(enemy);
+ }
+}
+// Initial enemy spawn
+spawnEnemies();
+// Score display
+var scoreText = new Text2('Coins: 0/' + totalCoins, {
+ size: 60,
+ fill: 0xFFFFFF
+});
+scoreText.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreText);
+// Level display
+var levelText = new Text2('Level: ' + currentLevel, {
+ size: 50,
+ fill: 0xFFFFFF
+});
+levelText.anchor.set(1, 0);
+LK.gui.topRight.addChild(levelText);
+// Movement controls
+var moveLeft = false;
+var moveRight = false;
+var moveUp = false;
+var moveDown = false;
+// Touch controls
+game.down = function (x, y, obj) {
+ var gamePos = game.toLocal(obj.parent.toGlobal(obj.position));
+ // Determine movement direction based on touch position relative to player
+ if (gamePos.x < player.x - 50) {
+ moveLeft = true;
+ } else if (gamePos.x > player.x + 50) {
+ moveRight = true;
+ }
+ // Check for ladder climbing
+ for (var i = 0; i < ladders.length; i++) {
+ var ladder = ladders[i];
+ if (Math.abs(gamePos.x - ladder.x) < 80 && Math.abs(gamePos.y - ladder.y) < 100) {
+ // Determine climb direction
+ if (gamePos.y < player.y) {
+ moveUp = true;
+ } else {
+ moveDown = true;
+ }
+ break;
+ }
+ }
+};
+game.up = function (x, y, obj) {
+ moveLeft = false;
+ moveRight = false;
+ moveUp = false;
+ moveDown = false;
+};
+// Game update loop
+game.update = function () {
+ // Handle player movement
+ if (!player.isClimbing) {
+ if (moveLeft) {
+ player.x -= player.speed;
+ }
+ if (moveRight) {
+ player.x += player.speed;
+ }
+ // Keep player on current platform
+ if (player.currentPlatform !== null) {
+ var currentPlatform = platforms[player.currentPlatform];
+ player.x = Math.max(currentPlatform.x - 250, Math.min(currentPlatform.x + 250, player.x));
+ }
+ // Check for ladder climbing
+ if (moveUp || moveDown) {
+ for (var i = 0; i < ladders.length; i++) {
+ var ladder = ladders[i];
+ if (Math.abs(player.x - ladder.x) < 40) {
+ player.isClimbing = true;
+ player.x = ladder.x; // Snap to ladder
+ // Find target platform
+ var targetPlatformIndex = -1;
+ if (moveUp) {
+ // Find platform above
+ for (var j = 0; j < platforms.length; j++) {
+ if (platforms[j].y < player.y && Math.abs(platforms[j].x - ladder.x) < 100) {
+ targetPlatformIndex = j;
+ break;
+ }
+ }
+ } else if (moveDown) {
+ // Find platform below
+ for (var j = 0; j < platforms.length; j++) {
+ if (platforms[j].y > player.y && Math.abs(platforms[j].x - ladder.x) < 100) {
+ targetPlatformIndex = j;
+ break;
+ }
+ }
+ }
+ if (targetPlatformIndex !== -1) {
+ player.targetY = platforms[targetPlatformIndex].y;
+ player.currentPlatform = targetPlatformIndex;
+ }
+ break;
+ }
+ }
+ }
+ }
+ // Check coin collection
+ for (var i = 0; i < coins.length; i++) {
+ var coin = coins[i];
+ if (!coin.collected && player.intersects(coin)) {
+ coin.collected = true;
+ coin.alpha = 0;
+ coinsCollected++;
+ LK.setScore(coinsCollected);
+ scoreText.setText('Coins: ' + coinsCollected + '/' + totalCoins);
+ LK.getSound('coinCollect').play();
+ // Check win condition
+ if (coinsCollected >= totalCoins) {
+ LK.showYouWin();
+ }
+ }
+ }
+ // Check enemy collisions
+ for (var i = 0; i < enemies.length; i++) {
+ var enemy = enemies[i];
+ if (player.intersects(enemy)) {
+ LK.getSound('enemyHit').play();
+ LK.effects.flashScreen(0xff0000, 500);
+ LK.showGameOver();
+ }
+ }
+ // Level progression (spawn more enemies every 30 seconds)
+ if (LK.ticks % (60 * 30) === 0 && coinsCollected < totalCoins) {
+ currentLevel++;
+ levelText.setText('Level: ' + currentLevel);
+ spawnEnemies();
+ }
+};
\ No newline at end of file
coin, hd colors. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
Scary blue background of forest, moon, small owl on tree, HD colors. In-Game asset. 2d. High contrast. No shadows
Scary graveyard with different things around it scary things spiders, web spider, skulls, swords, grass, trees.. In-Game asset. 2d. High contrast. No shadows
Scary wallpaper without characters, with all colors, HD colors. In-Game asset. 3d. High contrast. No shadows