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 var Missile = Container.expand(function () { var self = Container.call(this); var missileGraphics = self.attachAsset('missile', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.move = function () { self.y -= self.speed; }; }); // 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); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize assets used in the game. // Game variables var missiles = []; 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 - 200 })); } // Initialize score display function initScore() { scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); } // Fire a missile function fireMissile(x, y) { var missile = new Missile(); missile.x = x; missile.y = y; missiles.push(missile); game.addChild(missile); } // 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 m = missiles.length - 1; m >= 0; m--) { for (var a = aliens.length - 1; a >= 0; a--) { if (missiles[m].intersects(aliens[a])) { aliens[a].destroy(); aliens.splice(a, 1); missiles[m].destroy(); missiles.splice(m, 1); LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); break; } } } } // 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 fire missiles game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); fireMissile(pos.x, earth.y); }); // Game tick event LK.on('tick', function () { // Move missiles for (var i = missiles.length - 1; i >= 0; i--) { missiles[i].move(); if (missiles[i].y < -missiles[i].height) { missiles[i].destroy(); missiles.splice(i, 1); } } // 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; } // Check for collisions and game over checkCollisions(); checkGameOver(); }); // Initialize game elements initEarth(); initScore();
===================================================================
--- original.js
+++ change.js
@@ -1,142 +1,146 @@
-/****
+/****
* Classes
****/
// Missile class
var Missile = Container.expand(function () {
- var self = Container.call(this);
- var missileGraphics = self.attachAsset('missile', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 10;
- self.move = function () {
- self.y -= self.speed;
- };
+ var self = Container.call(this);
+ var missileGraphics = self.attachAsset('missile', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 10;
+ self.move = function () {
+ self.y -= self.speed;
+ };
});
// 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 () {
- self.y += self.speed;
- };
+ 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);
+ };
});
-/****
+/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000000 // Init game with black background
+ backgroundColor: 0x000000 // Init game with black background
});
-/****
+/****
* Game Code
****/
-// Game variables
// Initialize assets used in the game.
+// Game variables
var missiles = [];
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 - 200
- }));
+ earth = game.addChild(LK.getAsset('earth', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: game.width / 2,
+ y: game.height - 200
+ }));
}
// Initialize score display
function initScore() {
- scoreTxt = new Text2('0', {
- size: 150,
- fill: "#ffffff"
- });
- scoreTxt.anchor.set(0.5, 0);
- LK.gui.top.addChild(scoreTxt);
+ scoreTxt = new Text2('0', {
+ size: 150,
+ fill: "#ffffff"
+ });
+ scoreTxt.anchor.set(0.5, 0);
+ LK.gui.top.addChild(scoreTxt);
}
// Fire a missile
function fireMissile(x, y) {
- var missile = new Missile();
- missile.x = x;
- missile.y = y;
- missiles.push(missile);
- game.addChild(missile);
+ var missile = new Missile();
+ missile.x = x;
+ missile.y = y;
+ missiles.push(missile);
+ game.addChild(missile);
}
// 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);
+ 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 m = missiles.length - 1; m >= 0; m--) {
- for (var a = aliens.length - 1; a >= 0; a--) {
- if (missiles[m].intersects(aliens[a])) {
- aliens[a].destroy();
- aliens.splice(a, 1);
- missiles[m].destroy();
- missiles.splice(m, 1);
- LK.setScore(LK.getScore() + 1);
- scoreTxt.setText(LK.getScore());
- break;
- }
- }
- }
+ for (var m = missiles.length - 1; m >= 0; m--) {
+ for (var a = aliens.length - 1; a >= 0; a--) {
+ if (missiles[m].intersects(aliens[a])) {
+ aliens[a].destroy();
+ aliens.splice(a, 1);
+ missiles[m].destroy();
+ missiles.splice(m, 1);
+ LK.setScore(LK.getScore() + 1);
+ scoreTxt.setText(LK.getScore());
+ break;
+ }
+ }
+ }
}
// 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;
- }
- }
+ for (var i = 0; i < aliens.length; i++) {
+ if (aliens[i].intersects(earth)) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ break;
+ }
+ }
}
// Touch event to fire missiles
game.on('down', function (obj) {
- var pos = obj.event.getLocalPosition(game);
- fireMissile(pos.x, earth.y);
+ var pos = obj.event.getLocalPosition(game);
+ fireMissile(pos.x, earth.y);
});
// Game tick event
LK.on('tick', function () {
- // Move missiles
- for (var i = missiles.length - 1; i >= 0; i--) {
- missiles[i].move();
- if (missiles[i].y < -missiles[i].height) {
- missiles[i].destroy();
- missiles.splice(i, 1);
- }
- }
- // 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;
- }
- // Check for collisions and game over
- checkCollisions();
- checkGameOver();
+ // Move missiles
+ for (var i = missiles.length - 1; i >= 0; i--) {
+ missiles[i].move();
+ if (missiles[i].y < -missiles[i].height) {
+ missiles[i].destroy();
+ missiles.splice(i, 1);
+ }
+ }
+ // 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;
+ }
+ // Check for collisions and game over
+ checkCollisions();
+ checkGameOver();
});
// Initialize game elements
initEarth();
initScore();
\ No newline at end of file