Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: enemies is not defined' in or related to this line: 'enemies.splice(enemies.indexOf(self), 1);' Line Number: 87
User prompt
Please fix the bug: 'ReferenceError: enemies is not defined' in or related to this line: 'enemies.push(enemy);' Line Number: 171
User prompt
Please fix the bug: 'ReferenceError: enemies is not defined' in or related to this line: 'enemies.forEach(function (enemy) {' Line Number: 206
User prompt
rename the enemies array to allenemies
User prompt
Please fix the bug: 'ReferenceError: enemies is not defined' in or related to this line: 'enemies.splice(enemies.indexOf(self.target), 1);' Line Number: 34
User prompt
Please fix the bug: 'ReferenceError: enemies is not defined' in or related to this line: 'enemies.push(enemy);' Line Number: 172
User prompt
Please fix the bug: 'ReferenceError: enemies is not defined' in or related to this line: 'enemies.forEach(function (enemy) {' Line Number: 207
User prompt
rename the enemies array to allenemies
User prompt
remove from the enemies array the enemy touched by the bullet
User prompt
enemies.splice removes from the array enemies that were not touched by the bullet
User prompt
write line 87 differently
Code edit (4 edits merged)
Please save this source code
User prompt
Add 0.1 temporary invulnerability to enemies when hit by a bullet
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
After destroying an enemy with a bullet, new bullets do not appear
Code edit (5 edits merged)
Please save this source code
User prompt
The movement of monsters should only be on the road tiles from first to last
Code edit (5 edits merged)
Please save this source code
User prompt
The background is not visible
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
show the tower's shot radius when an enemy enters it, hide when it leaves the radius
Code edit (1 edits merged)
Please save this source code
/**** * Classes ****/ // Bullet class for projectiles fired by towers var Bullet = Container.expand(function (startX, startY, target) { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.target = target; self.x = startX; self.y = startY; self.update = function () { var dx = self.target.x - self.x; var dy = self.target.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < 10) { self.target.hit(); self.destroy(); } else { self.x += dx / distance * self.speed; self.y += dy / distance * self.speed; // Calculate the angle to the target var angle = Math.atan2(dy, dx); // Rotate the bullet towards the target bulletGraphics.rotation = angle + 90; } }; }); // Enemy class for the creatures attacking the realm var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.health = 100; self.path = [{ x: 2010, y: 2600 }, { x: 1000, y: 2600 }, { x: 1000, y: 200 }, { x: 0, y: 200 }]; self.pathIndex = 0; self.update = function () { var target = self.path[self.pathIndex]; var dx = target.x - self.x; var dy = target.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < self.speed) { self.x = target.x; self.y = target.y; self.pathIndex++; if (self.pathIndex >= self.path.length) { // Enemy reached the end, game over logic LK.showGameOver(); } } else { self.x += dx / distance * self.speed; self.y += dy / distance * self.speed; } }; self.hit = function () { self.health -= 50; if (self.health <= 0) { self.destroy(); enemies.splice(enemies.indexOf(self), 1); } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Tower class to represent defense towers var Tower = Container.expand(function () { var self = Container.call(this); var towerGraphics = self.attachAsset('tower', { anchorX: 0.5, anchorY: 0.5 }); self.range = 600; self.fireRate = 60; // Fire every 60 frames self.fireCooldown = 0; // The turret's firing range is hidden and not displayed on the game screen self.update = function () { if (self.fireCooldown < 0) { self.fireCooldown = 0; } if (self.fireCooldown > 0) { self.fireCooldown--; } }; self.canFire = function () { return self.fireCooldown === 0; }; self.fire = function (target) { if (self.canFire()) { var bullet = new Bullet(self.x, self.y, target); game.addChild(bullet); bullets.push(bullet); self.fireCooldown = self.fireRate; } else if (self.fireCooldown > 0) { self.fireCooldown--; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000, //Init game with black background fps: 60 }); /**** * Game Code ****/ // Add the background image to the game var background = LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 1366 }); game.addChild(background); // Initialize arrays and variables var towers = []; var bullets = []; var enemies = []; // Create and position towers function createTowers() { var tower1 = new Tower(); tower1.x = 500; tower1.y = 1366; game.addChild(tower1); towers.push(tower1); var tower2 = new Tower(); tower2.x = 1500; tower2.y = 1366; game.addChild(tower2); towers.push(tower2); } // Spawn enemies at intervals function spawnEnemy() { var enemy = new Enemy(); enemy.x = 2010; enemy.y = 200; game.addChild(enemy); enemies.push(enemy); } // Create a road for the enemies to follow function createRoad(x, y) { if (Array.isArray(x)) { for (var i = x[0]; i < x[1]; i += 100) { var roadBlock = LK.getAsset('road', { anchorX: 0.5, anchorY: 0.5, x: i, y: y }); game.addChild(roadBlock); } } else { for (var i = y[0]; i < y[1]; i += 100) { var roadBlock = LK.getAsset('road', { anchorX: 0.5, anchorY: 0.5, x: x, y: i }); game.addChild(roadBlock); } } } createRoad(2000, [200, 2601]); createRoad([1000, 2000], 2601); createRoad(1000, [200, 2601]); createRoad([0, 1000], 200); // Game update loop game.update = function () { // Update towers towers.forEach(function (tower) { tower.update(); enemies.forEach(function (enemy) { var dx = tower.x - enemy.x; var dy = tower.y - enemy.y; var distance = Math.sqrt(dx * dx + dy * dy); if (tower.canFire() && distance < tower.range) { tower.fire(enemy); } }); }); // Update bullets bullets.forEach(function (bullet) { bullet.update(); }); // Update enemies enemies.forEach(function (enemy) { enemy.update(); }); // Spawn enemies every 120 frames if (LK.ticks % 120 === 0) { spawnEnemy(); } }; // Initialize game elements createTowers();
===================================================================
--- original.js
+++ change.js
@@ -45,12 +45,12 @@
x: 1000,
y: 2600
}, {
x: 1000,
- y: 100
+ y: 200
}, {
x: 0,
- y: 100
+ y: 200
}];
self.pathIndex = 0;
self.update = function () {
var target = self.path[self.pathIndex];
@@ -65,18 +65,10 @@
// Enemy reached the end, game over logic
LK.showGameOver();
}
} else {
- // Only move if the next position is on a road
- var nextX = self.x + dx / distance * self.speed;
- var nextY = self.y + dy / distance * self.speed;
- var nextPositionIsOnRoad = game.children.some(function (child) {
- return child instanceof Road && Math.abs(child.x - nextX) < 50 && Math.abs(child.y - nextY) < 50;
- });
- if (nextPositionIsOnRoad) {
- self.x = nextX;
- self.y = nextY;
- }
+ self.x += dx / distance * self.speed;
+ self.y += dy / distance * self.speed;
}
};
self.hit = function () {
self.health -= 50;
@@ -85,18 +77,8 @@
enemies.splice(enemies.indexOf(self), 1);
}
};
});
-// Road class for the path that enemies will follow
-var Road = Container.expand(function (x, y) {
- var self = Container.call(this);
- var roadGraphics = self.attachAsset('road', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.x = x;
- self.y = y;
-});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Tower class to represent defense towers
var Tower = Container.expand(function () {
@@ -180,14 +162,24 @@
// Create a road for the enemies to follow
function createRoad(x, y) {
if (Array.isArray(x)) {
for (var i = x[0]; i < x[1]; i += 100) {
- var roadBlock = new Road(i, y);
+ var roadBlock = LK.getAsset('road', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: i,
+ y: y
+ });
game.addChild(roadBlock);
}
} else {
for (var i = y[0]; i < y[1]; i += 100) {
- var roadBlock = new Road(x, i);
+ var roadBlock = LK.getAsset('road', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: x,
+ y: i
+ });
game.addChild(roadBlock);
}
}
}
No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat. Goblin. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat. Tower with ballista. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
One bow arrow facing upwards. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
texture of the road made of large stones like tiles for a game. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Top view. Texture for 2D games. Green meadow.