User prompt
Fix Bug: 'ReferenceError: aliens is not defined' in or related to this line: 'aliens.push(asteroid);' Line Number: 111
User prompt
rename alien to asteroid
User prompt
shield should rotate on its axis, so that it is always facing the earth
User prompt
shield should roate on its axis to always keep the same incline while orbiting
User prompt
double shield speed
Code edit (1 edits merged)
Please save this source code
User prompt
aliens should spawn from anywhere as long as it is offscreen
User prompt
aliens should always spawn from outside the game margins
Code edit (2 edits merged)
Please save this source code
User prompt
if shield intersects alien then alien should be destroyed
User prompt
on tap change shiedls direction
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'shield')' in or related to this line: 'window.window.shield.update();' Line Number: 127
User prompt
Fix Bug: 'ReferenceError: shield is not defined' in or related to this line: 'shield.orbitAngle = Math.atan2(dy, dx);' Line Number: 108
User prompt
Fix Bug: 'ReferenceError: shield is not defined' in or related to this line: 'shield.update();' Line Number: 127
User prompt
Fix Bug: 'ReferenceError: shield is not defined' in or related to this line: 'shield.update();' Line Number: 127
User prompt
crete a shiedl class. shiedl will orbit the earth and will move on touch.
Code edit (1 edits merged)
Please save this source code
User prompt
remove missils
User prompt
aliens should move towards the earth
Initial prompt
Misil defense
/**** * Classes ****/ // Missile class // Alien class var Alien = Container.expand(function () { var self = Container.call(this); var alienGraphics = self.attachAsset('alien', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.move = function () { var dy = earth.y - self.y; var dx = earth.x - self.x; var angle = Math.atan2(dy, dx); self.x += self.speed * Math.cos(angle); self.y += self.speed * Math.sin(angle); }; }); // Shield class var Shield = Container.expand(function () { var self = Container.call(this); self.attachAsset('shield', { anchorX: 0.5, anchorY: 0.5 }); self.orbitRadius = 200; self.orbitSpeed = 0.02; self.orbitAngle = 0; self.update = function () { self.orbitAngle += self.orbitSpeed; self.x = earth.x + self.orbitRadius * Math.cos(self.orbitAngle); self.y = earth.y + self.orbitRadius * Math.sin(self.orbitAngle); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Game variables // Initialize assets used in the game. var aliens = []; var earth; var scoreTxt; var spawnAlienInterval = 120; // Frames until a new alien spawns var nextAlienSpawn = spawnAlienInterval; // Initialize Earth function initEarth() { earth = game.addChild(LK.getAsset('earth', { anchorX: 0.5, anchorY: 0.5, x: game.width / 2, y: game.height - 1000 })); window.shield = game.addChild(new Shield()); window.shield.x = earth.x; window.shield.y = earth.y - window.shield.orbitRadius; } // Initialize score display function initScore() { scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); } // Spawn an alien function spawnAlien() { var alien = new Alien(); alien.x = Math.random() * (game.width - alien.width) + alien.width / 2; alien.y = -alien.height / 2; aliens.push(alien); game.addChild(alien); } // Check for collisions function checkCollisions() { for (var i = 0; i < aliens.length; i++) { if (window.shield.intersects(aliens[i])) { aliens[i].destroy(); aliens.splice(i, 1); } } } // Game over check function checkGameOver() { for (var i = 0; i < aliens.length; i++) { if (aliens[i].intersects(earth)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); break; } } } // Touch event to move shield game.on('down', function (obj) { window.shield.orbitSpeed *= -1; }); // Game tick event LK.on('tick', function () { // Move aliens for (var j = aliens.length - 1; j >= 0; j--) { aliens[j].move(); if (aliens[j].y > game.height + aliens[j].height) { aliens[j].destroy(); aliens.splice(j, 1); } } // Spawn aliens nextAlienSpawn--; if (nextAlienSpawn <= 0) { spawnAlien(); nextAlienSpawn = spawnAlienInterval; } // Update shield position window.shield.update(); // Check for collisions and game over checkCollisions(); checkGameOver(); }); // Initialize game elements initEarth(); initScore();
===================================================================
--- original.js
+++ change.js
@@ -81,9 +81,16 @@
aliens.push(alien);
game.addChild(alien);
}
// Check for collisions
-function checkCollisions() {}
+function checkCollisions() {
+ for (var i = 0; i < aliens.length; i++) {
+ if (window.shield.intersects(aliens[i])) {
+ aliens[i].destroy();
+ aliens.splice(i, 1);
+ }
+ }
+}
// Game over check
function checkGameOver() {
for (var i = 0; i < aliens.length; i++) {
if (aliens[i].intersects(earth)) {