User prompt
make it so bullets dont hit dead enemies
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'destroy')' in or related to this line: 'bullets[j].destroy();' Line Number: 176
User prompt
make it so bullets dont get stuck on an enemy after it dies
User prompt
make enemies spawn in more often
User prompt
make a boss enemy that spawns in every once in a while with more health
User prompt
have enemies spawn
User prompt
make all enemies move towards player
User prompt
go back to before the sationary enemies
User prompt
the enemies are not spawning
User prompt
get rid of hero movement with keyboard
User prompt
make the enemies spawn from the top of the screen
User prompt
make enemies that come towards the player
User prompt
get rid of the spawn points
User prompt
make all enemies go towards the player
User prompt
bulletss are hitting dead enemies
User prompt
bullets keep getting locked at the closest enemy position
User prompt
make it so that only one enemy spawns at each position
User prompt
make stationary enemies spawn in a grid towards the top of the screen
User prompt
have enemies spawn in at certain positions and stay there
User prompt
fix bullets so they dont get stuck at a certain y
User prompt
make hero move with mouse but bound to same y level
Code edit (1 edits merged)
Please save this source code
User prompt
delete pause button
User prompt
pauseGame() is broken
User prompt
make it so p pauses
===================================================================
--- original.js
+++ change.js
@@ -23,22 +23,19 @@
anchorX: 0.5,
anchorY: 0.5
});
self.health = 50;
+ self.speed = 3;
self.update = function () {
// Update logic for enemy
- // Calculate direction towards hero
+ // Move towards hero
var dx = hero.x - self.x;
var dy = hero.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
- // Normalize the direction
if (distance > 0) {
- dx /= distance;
- dy /= distance;
+ self.x += self.speed * dx / distance;
+ self.y += self.speed * dy / distance;
}
- // Move towards the hero
- self.x += dx * self.speed;
- self.y += dy * self.speed;
};
self.takeDamage = function (amount) {
self.health -= amount;
if (self.health <= 0) {
@@ -46,16 +43,17 @@
}
};
self.die = function () {
// Handle enemy death
+ for (var i = bullets.length - 1; i >= 0; i--) {
+ if (bullets[i].intersects(self)) {
+ bullets[i].destroy();
+ bullets.splice(i, 1);
+ }
+ }
self.destroy();
};
});
-// BossEnemy class
-var BossEnemy = Enemy.expand(function () {
- var self = Enemy.call(this);
- self.health = 200; // BossEnemy has more health
-});
//<Assets used in the game will automatically appear here>
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
@@ -101,13 +99,20 @@
// Initialize hero
var hero = game.addChild(new Hero());
hero.x = 2048 / 2;
hero.y = 2732 - 200;
-// Initialize enemies array and spawn position
+// Initialize enemies array
var enemies = [];
-var spawnY = 0;
// Initialize bullets array
var bullets = [];
+// Function to spawn enemies
+function spawnEnemy() {
+ var enemy = new Enemy();
+ enemy.x = Math.random() * 2048;
+ enemy.y = 0;
+ enemies.push(enemy);
+ game.addChild(enemy);
+}
// Function to shoot bullets
function shootBullet() {
var bullet = new Bullet();
bullet.x = hero.x;
@@ -123,43 +128,54 @@
// Update game logic
game.update = function () {
// Update hero
hero.update();
+ // Handle keyboard events for hero movement and game pause
+ LK.on('keydown', function (event) {
+ switch (event.key) {
+ case 'w':
+ // Move hero up
+ hero.y -= hero.speed;
+ break;
+ case 'a':
+ // Move hero left
+ hero.x -= hero.speed;
+ break;
+ case 's':
+ // Move hero down
+ hero.y += hero.speed;
+ break;
+ case 'd':
+ // Move hero right
+ hero.x += hero.speed;
+ break;
+ }
+ });
// Update enemies
for (var i = enemies.length - 1; i >= 0; i--) {
enemies[i].update();
if (enemies[i].intersects(hero)) {
hero.takeDamage(10);
enemies[i].destroy();
enemies.splice(i, 1);
}
- // Spawn new enemy at the top of the screen every 30 frames
- if (LK.ticks % 30 == 0) {
- var enemy;
- if (LK.ticks % 600 == 0) {
- // Spawn BossEnemy every 600 frames
- enemy = new BossEnemy();
- } else {
- enemy = new Enemy();
- }
- enemy.x = Math.random() * 2048; // Random x position
- enemy.y = 0; // Spawn at the top of the screen
- enemies.push(enemy);
- game.addChild(enemy);
- }
}
// Update bullets
for (var j = bullets.length - 1; j >= 0; j--) {
bullets[j].update();
for (var k = enemies.length - 1; k >= 0; k--) {
- if (bullets[j] && enemies[k].health > 0 && bullets[j].intersects(enemies[k])) {
+ if (bullets[j].intersects(enemies[k])) {
enemies[k].takeDamage(20);
bullets[j].destroy();
bullets.splice(j, 1);
break;
}
}
}
+ // Spawn enemies periodically
+ if (LK.ticks % 60 == 0) {
+ spawnEnemy();
+ }
};
game.move = function (x, y, obj) {
hero.x = x;
};
\ No newline at end of file