User prompt
Please fix the bug: 'Timeout.tick error: Cannot read properties of undefined (reading 'destroy')' in or related to this line: 'enemies[j].destroy();' Line Number: 198
User prompt
enemy ships should dissaper with same time as destroy
User prompt
make it 0.2 second
User prompt
make it 0.5 second
User prompt
destroy asset should dissappear after 1 second
User prompt
everytime laser hits a target use destroy asset to destroy ship
User prompt
add stars on background using star asset
User prompt
add laser sound again
User prompt
add laser sound again
User prompt
add background using background asset
User prompt
player should only able to move right and left
User prompt
add laser shooting sound using the sound i add
Initial prompt
space ship advanture
/**** * Classes ****/ // EnemyLaser class var EnemyLaser = Container.expand(function (x, y) { var self = Container.call(this); var enemyLaserGraphics = self.attachAsset('enemylaser', { anchorX: 0.5, anchorY: 0.5 }); self.x = x; self.y = y; self.speed = 10; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; }); // EnemyShip class var EnemyShip = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemyship', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.destroy(); } }; self.shoot = function () { var enemyLaser = new EnemyLaser(self.x, self.y + enemyGraphics.height / 2); game.addChild(enemyLaser); enemyLasers.push(enemyLaser); }; }); // Laser class var Laser = Container.expand(function (x, y) { var self = Container.call(this); var laserGraphics = self.attachAsset('laser', { anchorX: 0.5, anchorY: 0.5 }); self.x = x; self.y = y; self.speed = -15; self.update = function () { self.y += self.speed; if (self.y < 0) { self.destroy(); } }; }); //<Assets used in the game will automatically appear here> // SpaceShip class var SpaceShip = Container.expand(function () { var self = Container.call(this); var shipGraphics = self.attachAsset('spaceship', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { self.y = 2732 - 200; }; self.shoot = function () { var laser = new Laser(self.x, self.y - shipGraphics.height / 2); game.addChild(laser); lasers.push(laser); LK.getSound('lasersound').play(); }; }); // Star class var Star = Container.expand(function () { var self = Container.call(this); var starGraphics = self.attachAsset('star', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -starGraphics.height; self.x = Math.random() * 2048; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var spaceship; var enemies = []; var lasers = []; var enemyLasers = []; var stars = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Create spaceship spaceship = new SpaceShip(); spaceship.x = 2048 / 2; spaceship.y = 2732 - 200; game.addChild(spaceship); // Create and add stars for (var i = 0; i < 50; i++) { var star = new Star(); star.x = Math.random() * 2048; star.y = Math.random() * 2732; stars.push(star); game.addChild(star); } // Spawn enemies function spawnEnemy() { var enemy = new EnemyShip(); enemy.x = Math.random() * 2048; enemy.y = -100; enemies.push(enemy); game.addChild(enemy); } // Handle game updates game.update = function () { // Update spaceship spaceship.update(); // Update stars for (var i = stars.length - 1; i >= 0; i--) { stars[i].update(); } // Update lasers for (var i = lasers.length - 1; i >= 0; i--) { lasers[i].update(); if (lasers[i].y < 0) { lasers[i].destroy(); lasers.splice(i, 1); } } // Update enemy lasers for (var i = enemyLasers.length - 1; i >= 0; i--) { enemyLasers[i].update(); if (enemyLasers[i].y > 2732) { enemyLasers[i].destroy(); enemyLasers.splice(i, 1); } } // Update enemies for (var i = enemies.length - 1; i >= 0; i--) { enemies[i].update(); if (enemies[i].y > 2732) { enemies[i].destroy(); enemies.splice(i, 1); } } // Check for collisions for (var i = lasers.length - 1; i >= 0; i--) { for (var j = enemies.length - 1; j >= 0; j--) { if (lasers[i].intersects(enemies[j])) { lasers[i].destroy(); var destroyEffect = LK.getAsset('destroy', { anchorX: 0.5, anchorY: 0.5, x: enemies[j].x, y: enemies[j].y }); game.addChild(destroyEffect); LK.setTimeout(function () { destroyEffect.destroy(); }, 200); (function (enemyIndex, laserIndex) { LK.setTimeout(function () { if (enemies[enemyIndex]) { enemies[enemyIndex].destroy(); enemies.splice(enemyIndex, 1); } if (lasers[laserIndex]) { lasers.splice(laserIndex, 1); } score++; scoreTxt.setText(score); }, 200); })(j, i); break; } } } for (var i = enemyLasers.length - 1; i >= 0; i--) { if (enemyLasers[i].intersects(spaceship)) { enemyLasers[i].destroy(); enemyLasers.splice(i, 1); // Flash screen red for 1 second (1000ms) to show we are dead. LK.effects.flashScreen(0xff0000, 1000); // Show game over. The game will be automatically paused while game over is showing. LK.showGameOver(); // Calling this will destroy the 'Game' and reset entire game state. break; } } // Spawn enemies periodically if (LK.ticks % 60 == 0) { spawnEnemy(); } // Enemies shoot periodically if (LK.ticks % 120 == 0) { for (var i = 0; i < enemies.length; i++) { enemies[i].shoot(); } } }; // Handle touch events game.down = function (x, y, obj) { spaceship.shoot(); }; game.move = function (x, y, obj) { spaceship.x = x; }; game.up = function (x, y, obj) { // No action needed on touch up };
===================================================================
--- original.js
+++ change.js
@@ -181,15 +181,21 @@
game.addChild(destroyEffect);
LK.setTimeout(function () {
destroyEffect.destroy();
}, 200);
- LK.setTimeout(function () {
- enemies[j].destroy();
- lasers.splice(i, 1);
- enemies.splice(j, 1);
- score++;
- scoreTxt.setText(score);
- }, 200);
+ (function (enemyIndex, laserIndex) {
+ LK.setTimeout(function () {
+ if (enemies[enemyIndex]) {
+ enemies[enemyIndex].destroy();
+ enemies.splice(enemyIndex, 1);
+ }
+ if (lasers[laserIndex]) {
+ lasers.splice(laserIndex, 1);
+ }
+ score++;
+ scoreTxt.setText(score);
+ }, 200);
+ })(j, i);
break;
}
}
}
star white. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
enemy space ship boss it should be big and looking from top. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
explosion. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
linear laser red horizontal. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
red pixel circle with black background and hearth on middle. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
red circle with black background and bullet on middle next to x2 symbol. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.