User prompt
make space pirates fire missiles at truck
User prompt
make background black, add red, blue, and yellow stars
User prompt
make background white
User prompt
need more space pirates
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'intersects')' in or related to this line: 'if (bullets[i].intersects(spaceDOTs[j])) {' Line Number: 146
User prompt
truck should bullets at the space pirates for a one shot kill, spaceDOTs need 3 shots to kill. include health bat above spaceDOT
User prompt
space pirates should come in waves, starting with 3 and increasing by 2 every wave
User prompt
delete cargo
User prompt
cargo should be attached to truck
User prompt
cargo should stay right behind the truck
User prompt
truck and cargo should follow curser
User prompt
cargo should stick to the truck and not detatch
User prompt
cargo should be attached to the back of the truck
Initial prompt
Space Trucker
/**** * Classes ****/ // Bullet class var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.y += self.speed; }; }); // 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 = 5; self.update = function () { self.y += self.speed; }; }); // Space DOT class var SpaceDOT = Container.expand(function () { var self = Container.call(this); var dotGraphics = self.attachAsset('spaceDOT', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.health = 3; self.healthBar = self.attachAsset('healthBar', { anchorX: 0.5, anchorY: 0.5 }); self.healthBar.scaleX = self.health / 3; self.update = function () { self.y += self.speed; self.healthBar.scaleX = self.health / 3; if (self.y > 2732) { self.y = -self.height; } }; }); // Space Pirate class var SpacePirate = Container.expand(function () { var self = Container.call(this); var pirateGraphics = self.attachAsset('spacePirate', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { self.y += self.speed; if (self.y > 2732) { self.y = -self.height; } // Space pirates fire missiles if (LK.ticks % 60 == 0) { var newMissile = new Missile(); newMissile.x = self.x; newMissile.y = self.y; missiles.push(newMissile); game.addChild(newMissile); } }; }); //<Assets used in the game will automatically appear here> // Truck class var Truck = Container.expand(function () { var self = Container.call(this); var truckGraphics = self.attachAsset('truck', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { // Truck movement logic if (LK.ticks % 30 == 0) { var newBullet = new Bullet(); newBullet.x = self.x; newBullet.y = self.y; bullets.push(newBullet); game.addChild(newBullet); } }; self.move = function (x, y, obj) { self.x = x; self.y = y; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize stars for (var i = 0; i < 100; i++) { var redStar = game.addChild(LK.getAsset('redStar', { x: Math.random() * 2048, y: Math.random() * 2732 })); var blueStar = game.addChild(LK.getAsset('blueStar', { x: Math.random() * 2048, y: Math.random() * 2732 })); var yellowStar = game.addChild(LK.getAsset('yellowStar', { x: Math.random() * 2048, y: Math.random() * 2732 })); } // Initialize bullets var bullets = []; // Initialize missiles var missiles = []; // Initialize truck var truck = game.addChild(new Truck()); truck.x = 2048 / 2; truck.y = 2732 - 200; // Initialize space pirates var spacePirates = []; var currentWave = 1; for (var i = 0; i < 3; i++) { var pirate = new SpacePirate(); pirate.x = Math.random() * 2048; pirate.y = Math.random() * -2732; spacePirates.push(pirate); game.addChild(pirate); } // Initialize space DOTs var spaceDOTs = []; for (var i = 0; i < 5; i++) { var dot = new SpaceDOT(); dot.x = Math.random() * 2048; dot.y = Math.random() * -2732; spaceDOTs.push(dot); game.addChild(dot); } // Handle truck movement game.move = function (x, y, obj) { truck.x = x; truck.y = y; }; // Update game logic game.update = function () { // Update game logic // Update bullets for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); // Check collision with space pirates for (var j = spacePirates.length - 1; j >= 0; j--) { if (bullets[i].intersects(spacePirates[j])) { game.removeChild(bullets[i]); game.removeChild(spacePirates[j]); bullets.splice(i, 1); spacePirates.splice(j, 1); break; } } // Check collision with spaceDOTs for (var j = spaceDOTs.length - 1; j >= 0; j--) { if (bullets[i] && bullets[i].intersects(spaceDOTs[j])) { game.removeChild(bullets[i]); bullets.splice(i, 1); spaceDOTs[j].health -= 1; if (spaceDOTs[j].health <= 0) { game.removeChild(spaceDOTs[j]); spaceDOTs.splice(j, 1); } break; } } } for (var i = 0; i < spacePirates.length; i++) { spacePirates[i].update(); if (truck.intersects(spacePirates[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } // If all space pirates are defeated, increase the wave and the number of space pirates if (spacePirates.length == 0) { currentWave++; for (var j = 0; j < currentWave * 3 + 1; j++) { var pirate = new SpacePirate(); pirate.x = Math.random() * 2048; pirate.y = Math.random() * -2732; spacePirates.push(pirate); game.addChild(pirate); } } } // Update space DOTs for (var i = 0; i < spaceDOTs.length; i++) { spaceDOTs[i].update(); if (truck.intersects(spaceDOTs[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Update missiles for (var i = missiles.length - 1; i >= 0; i--) { missiles[i].update(); // Check collision with truck if (missiles[i].intersects(truck)) { game.removeChild(missiles[i]); missiles.splice(i, 1); LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } };
===================================================================
--- original.js
+++ change.js
@@ -12,8 +12,20 @@
self.update = function () {
self.y += self.speed;
};
});
+// 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 = 5;
+ self.update = function () {
+ self.y += self.speed;
+ };
+});
// Space DOT class
var SpaceDOT = Container.expand(function () {
var self = Container.call(this);
var dotGraphics = self.attachAsset('spaceDOT', {
@@ -47,8 +59,16 @@
self.y += self.speed;
if (self.y > 2732) {
self.y = -self.height;
}
+ // Space pirates fire missiles
+ if (LK.ticks % 60 == 0) {
+ var newMissile = new Missile();
+ newMissile.x = self.x;
+ newMissile.y = self.y;
+ missiles.push(newMissile);
+ game.addChild(newMissile);
+ }
};
});
//<Assets used in the game will automatically appear here>
// Truck class
@@ -101,8 +121,10 @@
}));
}
// Initialize bullets
var bullets = [];
+// Initialize missiles
+var missiles = [];
// Initialize truck
var truck = game.addChild(new Truck());
truck.x = 2048 / 2;
truck.y = 2732 - 200;
@@ -185,5 +207,16 @@
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
+ // Update missiles
+ for (var i = missiles.length - 1; i >= 0; i--) {
+ missiles[i].update();
+ // Check collision with truck
+ if (missiles[i].intersects(truck)) {
+ game.removeChild(missiles[i]);
+ missiles.splice(i, 1);
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
+ }
};
\ No newline at end of file