User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'setText')' in or related to this line: 'levelTxt.setText('Level ' + levelNum);' Line Number: 2583
User prompt
Create 50 levels.
Code edit (1 edits merged)
Please save this source code
User prompt
Portal Jump
Initial prompt
2d portal game
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Exit = Container.expand(function () { var self = Container.call(this); var exitGraphics = self.attachAsset('exit', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Glow effect var alpha = 0.7 + Math.sin(LK.ticks * 0.05) * 0.3; exitGraphics.alpha = alpha; }; return self; }); var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Rotate obstacle for visual effect obstacleGraphics.rotation += 0.02; }; return self; }); var Platform = Container.expand(function () { var self = Container.call(this); 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: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.onGround = false; self.speed = 8; self.jumpPower = 15; self.gravity = 0.8; self.maxVelocityY = 20; self.canTeleport = true; self.update = function () { // Apply gravity if (!self.onGround) { self.velocityY += self.gravity; if (self.velocityY > self.maxVelocityY) { self.velocityY = self.maxVelocityY; } } // Apply velocity self.x += self.velocityX; self.y += self.velocityY; // Apply friction self.velocityX *= 0.85; // Keep player in bounds if (self.x < 30) { self.x = 30; self.velocityX = 0; } if (self.x > 2018) { self.x = 2018; self.velocityX = 0; } // Check ground collision var groundY = 2732 - 100 - 30; // Ground top minus half player height if (self.y >= groundY) { self.y = groundY; self.velocityY = 0; self.onGround = true; } else { self.onGround = false; } // Check platform collisions for (var i = 0; i < platforms.length; i++) { var platform = platforms[i]; if (self.intersects(platform)) { var playerBottom = self.y + 30; var playerTop = self.y - 30; var platformTop = platform.y - 20; var platformBottom = platform.y + 20; if (self.velocityY > 0 && playerTop < platformTop && playerBottom > platformTop) { self.y = platformTop - 30; self.velocityY = 0; self.onGround = true; } } } // Check portal collisions if (self.canTeleport && portal1 && portal2) { if (self.intersects(portal1)) { self.teleport(portal1, portal2); } else if (self.intersects(portal2)) { self.teleport(portal2, portal1); } } // Check obstacle collisions for (var i = 0; i < obstacles.length; i++) { var obstacle = obstacles[i]; if (self.intersects(obstacle)) { self.die(); return; } } // Check exit collision if (exit && self.intersects(exit)) { self.win(); } }; self.teleport = function (fromPortal, toPortal) { self.canTeleport = false; self.x = toPortal.x; self.y = toPortal.y; // Add some visual feedback LK.effects.flashObject(self, 0x9C27B0, 300); LK.getSound('portal_travel').play(); // Prevent immediate re-teleport LK.setTimeout(function () { self.canTeleport = true; }, 500); }; self.jump = function () { if (self.onGround) { self.velocityY = -self.jumpPower; self.onGround = false; } }; self.moveLeft = function () { self.velocityX = -self.speed; }; self.moveRight = function () { self.velocityX = self.speed; }; self.die = function () { LK.effects.flashScreen(0xFF0000, 1000); LK.getSound('death').play(); LK.setTimeout(function () { LK.showGameOver(); }, 1000); }; self.win = function () { LK.effects.flashScreen(0x00FF00, 1000); LK.getSound('win').play(); LK.setTimeout(function () { LK.showYouWin(); }, 1000); }; return self; }); var Portal = Container.expand(function () { var self = Container.call(this); var portalGraphics = self.attachAsset('portal', { anchorX: 0.5, anchorY: 0.5 }); self.active = false; self.update = function () { if (self.active) { // Rotate portal for visual effect portalGraphics.rotation += 0.05; // Pulse effect var scale = 1 + Math.sin(LK.ticks * 0.1) * 0.1; portalGraphics.scaleX = scale; portalGraphics.scaleY = scale; } }; self.activate = function () { self.active = true; portalGraphics.alpha = 1; tween(portalGraphics, { scaleX: 1.2, scaleY: 1.2 }, { duration: 200, easing: tween.easeOut, onFinish: function onFinish() { tween(portalGraphics, { scaleX: 1, scaleY: 1 }, { duration: 200, easing: tween.easeIn }); } }); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ var player; var portal1 = null; var portal2 = null; var platforms = []; var obstacles = []; var exit; var portalCount = 0; var leftPressed = false; var rightPressed = false; var jumpPressed = false; // Create ground var ground = game.addChild(LK.getAsset('ground', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 2732 - 50 })); // Create player player = game.addChild(new Player()); player.x = 100; player.y = 2732 - 200; // Create platforms var platform1 = game.addChild(new Platform()); platform1.x = 400; platform1.y = 2400; platforms.push(platform1); var platform2 = game.addChild(new Platform()); platform2.x = 800; platform2.y = 2200; platforms.push(platform2); var platform3 = game.addChild(new Platform()); platform3.x = 1200; platform3.y = 2000; platforms.push(platform3); var platform4 = game.addChild(new Platform()); platform4.x = 1600; platform4.y = 1800; platforms.push(platform4); // Create obstacles var obstacle1 = game.addChild(new Obstacle()); obstacle1.x = 600; obstacle1.y = 2632 - 100 - 20; obstacles.push(obstacle1); var obstacle2 = game.addChild(new Obstacle()); obstacle2.x = 1000; obstacle2.y = 2632 - 100 - 20; obstacles.push(obstacle2); var obstacle3 = game.addChild(new Obstacle()); obstacle3.x = platform2.x; obstacle3.y = platform2.y - 40; obstacles.push(obstacle3); // Create exit exit = game.addChild(new Exit()); exit.x = 1800; exit.y = 1720; // Create UI instructions var instructionTxt = new Text2('Tap to create portals\nHold sides to move\nHold top to jump', { size: 60, fill: 0xFFFFFF }); instructionTxt.anchor.set(0.5, 0); LK.gui.top.addChild(instructionTxt); instructionTxt.y = 100; // Control areas var leftControlArea = { x: 0, y: 0, width: 683, height: 2732 }; var rightControlArea = { x: 1365, y: 0, width: 683, height: 2732 }; var jumpControlArea = { x: 683, y: 0, width: 682, height: 400 }; function isPointInArea(x, y, area) { return x >= area.x && x <= area.x + area.width && y >= area.y && y <= area.y + area.height; } function createPortal(x, y) { if (portalCount === 0) { // Create first portal portal1 = game.addChild(new Portal()); portal1.x = x; portal1.y = y; portalCount = 1; LK.getSound('portal_create').play(); } else if (portalCount === 1) { // Create second portal and activate both portal2 = game.addChild(new Portal()); portal2.x = x; portal2.y = y; portal1.activate(); portal2.activate(); portalCount = 2; LK.getSound('portal_create').play(); } else { // Replace portals if (portal1) { portal1.destroy(); } if (portal2) { portal2.destroy(); } portal1 = game.addChild(new Portal()); portal1.x = x; portal1.y = y; portal2 = null; portalCount = 1; LK.getSound('portal_create').play(); } } game.down = function (x, y, obj) { if (isPointInArea(x, y, leftControlArea)) { leftPressed = true; } else if (isPointInArea(x, y, rightControlArea)) { rightPressed = true; } else if (isPointInArea(x, y, jumpControlArea)) { jumpPressed = true; } else { // Create portal in game area createPortal(x, y); } }; game.up = function (x, y, obj) { leftPressed = false; rightPressed = false; jumpPressed = false; }; game.update = function () { // Handle controls if (leftPressed) { player.moveLeft(); } if (rightPressed) { player.moveRight(); } if (jumpPressed) { player.jump(); } };
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,359 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Exit = Container.expand(function () {
+ var self = Container.call(this);
+ var exitGraphics = self.attachAsset('exit', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.update = function () {
+ // Glow effect
+ var alpha = 0.7 + Math.sin(LK.ticks * 0.05) * 0.3;
+ exitGraphics.alpha = alpha;
+ };
+ return self;
+});
+var Obstacle = Container.expand(function () {
+ var self = Container.call(this);
+ var obstacleGraphics = self.attachAsset('obstacle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.update = function () {
+ // Rotate obstacle for visual effect
+ obstacleGraphics.rotation += 0.02;
+ };
+ return self;
+});
+var Platform = Container.expand(function () {
+ var self = Container.call(this);
+ 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: 0.5
+ });
+ self.velocityX = 0;
+ self.velocityY = 0;
+ self.onGround = false;
+ self.speed = 8;
+ self.jumpPower = 15;
+ self.gravity = 0.8;
+ self.maxVelocityY = 20;
+ self.canTeleport = true;
+ self.update = function () {
+ // Apply gravity
+ if (!self.onGround) {
+ self.velocityY += self.gravity;
+ if (self.velocityY > self.maxVelocityY) {
+ self.velocityY = self.maxVelocityY;
+ }
+ }
+ // Apply velocity
+ self.x += self.velocityX;
+ self.y += self.velocityY;
+ // Apply friction
+ self.velocityX *= 0.85;
+ // Keep player in bounds
+ if (self.x < 30) {
+ self.x = 30;
+ self.velocityX = 0;
+ }
+ if (self.x > 2018) {
+ self.x = 2018;
+ self.velocityX = 0;
+ }
+ // Check ground collision
+ var groundY = 2732 - 100 - 30; // Ground top minus half player height
+ if (self.y >= groundY) {
+ self.y = groundY;
+ self.velocityY = 0;
+ self.onGround = true;
+ } else {
+ self.onGround = false;
+ }
+ // Check platform collisions
+ for (var i = 0; i < platforms.length; i++) {
+ var platform = platforms[i];
+ if (self.intersects(platform)) {
+ var playerBottom = self.y + 30;
+ var playerTop = self.y - 30;
+ var platformTop = platform.y - 20;
+ var platformBottom = platform.y + 20;
+ if (self.velocityY > 0 && playerTop < platformTop && playerBottom > platformTop) {
+ self.y = platformTop - 30;
+ self.velocityY = 0;
+ self.onGround = true;
+ }
+ }
+ }
+ // Check portal collisions
+ if (self.canTeleport && portal1 && portal2) {
+ if (self.intersects(portal1)) {
+ self.teleport(portal1, portal2);
+ } else if (self.intersects(portal2)) {
+ self.teleport(portal2, portal1);
+ }
+ }
+ // Check obstacle collisions
+ for (var i = 0; i < obstacles.length; i++) {
+ var obstacle = obstacles[i];
+ if (self.intersects(obstacle)) {
+ self.die();
+ return;
+ }
+ }
+ // Check exit collision
+ if (exit && self.intersects(exit)) {
+ self.win();
+ }
+ };
+ self.teleport = function (fromPortal, toPortal) {
+ self.canTeleport = false;
+ self.x = toPortal.x;
+ self.y = toPortal.y;
+ // Add some visual feedback
+ LK.effects.flashObject(self, 0x9C27B0, 300);
+ LK.getSound('portal_travel').play();
+ // Prevent immediate re-teleport
+ LK.setTimeout(function () {
+ self.canTeleport = true;
+ }, 500);
+ };
+ self.jump = function () {
+ if (self.onGround) {
+ self.velocityY = -self.jumpPower;
+ self.onGround = false;
+ }
+ };
+ self.moveLeft = function () {
+ self.velocityX = -self.speed;
+ };
+ self.moveRight = function () {
+ self.velocityX = self.speed;
+ };
+ self.die = function () {
+ LK.effects.flashScreen(0xFF0000, 1000);
+ LK.getSound('death').play();
+ LK.setTimeout(function () {
+ LK.showGameOver();
+ }, 1000);
+ };
+ self.win = function () {
+ LK.effects.flashScreen(0x00FF00, 1000);
+ LK.getSound('win').play();
+ LK.setTimeout(function () {
+ LK.showYouWin();
+ }, 1000);
+ };
+ return self;
+});
+var Portal = Container.expand(function () {
+ var self = Container.call(this);
+ var portalGraphics = self.attachAsset('portal', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.active = false;
+ self.update = function () {
+ if (self.active) {
+ // Rotate portal for visual effect
+ portalGraphics.rotation += 0.05;
+ // Pulse effect
+ var scale = 1 + Math.sin(LK.ticks * 0.1) * 0.1;
+ portalGraphics.scaleX = scale;
+ portalGraphics.scaleY = scale;
+ }
+ };
+ self.activate = function () {
+ self.active = true;
+ portalGraphics.alpha = 1;
+ tween(portalGraphics, {
+ scaleX: 1.2,
+ scaleY: 1.2
+ }, {
+ duration: 200,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ tween(portalGraphics, {
+ scaleX: 1,
+ scaleY: 1
+ }, {
+ duration: 200,
+ easing: tween.easeIn
+ });
+ }
+ });
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var player;
+var portal1 = null;
+var portal2 = null;
+var platforms = [];
+var obstacles = [];
+var exit;
+var portalCount = 0;
+var leftPressed = false;
+var rightPressed = false;
+var jumpPressed = false;
+// Create ground
+var ground = game.addChild(LK.getAsset('ground', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 1024,
+ y: 2732 - 50
+}));
+// Create player
+player = game.addChild(new Player());
+player.x = 100;
+player.y = 2732 - 200;
+// Create platforms
+var platform1 = game.addChild(new Platform());
+platform1.x = 400;
+platform1.y = 2400;
+platforms.push(platform1);
+var platform2 = game.addChild(new Platform());
+platform2.x = 800;
+platform2.y = 2200;
+platforms.push(platform2);
+var platform3 = game.addChild(new Platform());
+platform3.x = 1200;
+platform3.y = 2000;
+platforms.push(platform3);
+var platform4 = game.addChild(new Platform());
+platform4.x = 1600;
+platform4.y = 1800;
+platforms.push(platform4);
+// Create obstacles
+var obstacle1 = game.addChild(new Obstacle());
+obstacle1.x = 600;
+obstacle1.y = 2632 - 100 - 20;
+obstacles.push(obstacle1);
+var obstacle2 = game.addChild(new Obstacle());
+obstacle2.x = 1000;
+obstacle2.y = 2632 - 100 - 20;
+obstacles.push(obstacle2);
+var obstacle3 = game.addChild(new Obstacle());
+obstacle3.x = platform2.x;
+obstacle3.y = platform2.y - 40;
+obstacles.push(obstacle3);
+// Create exit
+exit = game.addChild(new Exit());
+exit.x = 1800;
+exit.y = 1720;
+// Create UI instructions
+var instructionTxt = new Text2('Tap to create portals\nHold sides to move\nHold top to jump', {
+ size: 60,
+ fill: 0xFFFFFF
+});
+instructionTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(instructionTxt);
+instructionTxt.y = 100;
+// Control areas
+var leftControlArea = {
+ x: 0,
+ y: 0,
+ width: 683,
+ height: 2732
+};
+var rightControlArea = {
+ x: 1365,
+ y: 0,
+ width: 683,
+ height: 2732
+};
+var jumpControlArea = {
+ x: 683,
+ y: 0,
+ width: 682,
+ height: 400
+};
+function isPointInArea(x, y, area) {
+ return x >= area.x && x <= area.x + area.width && y >= area.y && y <= area.y + area.height;
+}
+function createPortal(x, y) {
+ if (portalCount === 0) {
+ // Create first portal
+ portal1 = game.addChild(new Portal());
+ portal1.x = x;
+ portal1.y = y;
+ portalCount = 1;
+ LK.getSound('portal_create').play();
+ } else if (portalCount === 1) {
+ // Create second portal and activate both
+ portal2 = game.addChild(new Portal());
+ portal2.x = x;
+ portal2.y = y;
+ portal1.activate();
+ portal2.activate();
+ portalCount = 2;
+ LK.getSound('portal_create').play();
+ } else {
+ // Replace portals
+ if (portal1) {
+ portal1.destroy();
+ }
+ if (portal2) {
+ portal2.destroy();
+ }
+ portal1 = game.addChild(new Portal());
+ portal1.x = x;
+ portal1.y = y;
+ portal2 = null;
+ portalCount = 1;
+ LK.getSound('portal_create').play();
+ }
+}
+game.down = function (x, y, obj) {
+ if (isPointInArea(x, y, leftControlArea)) {
+ leftPressed = true;
+ } else if (isPointInArea(x, y, rightControlArea)) {
+ rightPressed = true;
+ } else if (isPointInArea(x, y, jumpControlArea)) {
+ jumpPressed = true;
+ } else {
+ // Create portal in game area
+ createPortal(x, y);
+ }
+};
+game.up = function (x, y, obj) {
+ leftPressed = false;
+ rightPressed = false;
+ jumpPressed = false;
+};
+game.update = function () {
+ // Handle controls
+ if (leftPressed) {
+ player.moveLeft();
+ }
+ if (rightPressed) {
+ player.moveRight();
+ }
+ if (jumpPressed) {
+ player.jump();
+ }
+};
\ No newline at end of file