User prompt
Fix Bug: 'Uncaught ReferenceError: k is not defined' in this line: 'k;' Line Number: 125
User prompt
make sure blue bullet does not delete all enemies only the one it touches
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'Uncaught TypeError: window.addEventListener is not a function' in this line: 'window.addEventListener('keydown', function (e) {' Line Number: 68
User prompt
make sure when you press hotkey k you shoot a blue square and if it tocuhes an enemie it get deleted
Initial prompt
super power battle
/**** * Classes ****/ // Hero class var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.createAsset('hero', 'Hero character', 0.5, 0.5); self.shoot = function () { // Hero shoot logic }; self.update = function () { // Hero update logic }; }); // Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.createAsset('enemy', 'Enemy character', 0.5, 0.5); self.speed = 2; self.move = function () { self.y += self.speed; }; }); // BlueSquareBullet class var BlueSquareBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.createAsset('blueSquare', 'Blue Square Bullet', 0.5, 0.5); bulletGraphics.tint = 0x0000ff; // Tint the bullet blue self.speed = -10; self.move = function () { self.y += self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize important asset arrays var bullets = []; var enemies = []; // Create hero instance var hero = game.addChild(new Hero()); hero.x = 2048 / 2; hero.y = 2732 - 200; // Position hero near the bottom of the screen // Score display var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Game logic var isGameOver = false; var score = 0; // Event listeners game.on('down', function (obj) { var touchPos = obj.event.getLocalPosition(game); hero.x = touchPos.x; hero.shoot(); }); window.addEventListener('keydown', function (e) { if (e.key === 'k' || e.key === 'K') { hero.shoot(); } }); LK.on('tick', function () { if (!isGameOver) { // Update hero hero.update(); // Move and check bullets for (var b = bullets.length - 1; b >= 0; b--) { bullets[b].move(); if (bullets[b].y < 0) { bullets[b].destroy(); bullets.splice(b, 1); } else { // Check for bullet-enemy collisions for (var e = enemies.length - 1; e >= 0; e--) { if (bullets[b].intersects(enemies[e])) { score += 1; scoreTxt.setText(score.toString()); enemies[e].destroy(); enemies.splice(e, 1); bullets[b].destroy(); bullets.splice(b, 1); break; } } } } // Move and check enemies for (var e = enemies.length - 1; e >= 0; e--) { enemies[e].move(); if (enemies[e].y > 2732) { enemies[e].destroy(); enemies.splice(e, 1); } else if (enemies[e].intersects(hero)) { isGameOver = true; LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Spawn enemies if (LK.ticks % 120 == 0) { var newEnemy = new Enemy(); newEnemy.x = Math.random() * 2048; newEnemy.y = -50; enemies.push(newEnemy); game.addChild(newEnemy); } } }); // Hero shoot method Hero.prototype.shoot = function () { var newBullet = new BlueSquareBullet(); newBullet.x = this.x; newBullet.y = this.y; bullets.push(newBullet); game.addChild(newBullet); }; // Hero update method Hero.prototype.update = function () { // Add hero update logic if needed }; // Enemy move method Enemy.prototype.move = function () { this.y += this.speed; }; // Bullet move method Bullet.prototype.move = function () { this.y += this.speed; };
===================================================================
--- original.js
+++ change.js
@@ -1,45 +1,46 @@
-/****
+/****
* Classes
****/
// Hero class
var Hero = Container.expand(function () {
- var self = Container.call(this);
- var heroGraphics = self.createAsset('hero', 'Hero character', 0.5, 0.5);
- self.shoot = function () {
- // Hero shoot logic
- };
- self.update = function () {
- // Hero update logic
- };
+ var self = Container.call(this);
+ var heroGraphics = self.createAsset('hero', 'Hero character', 0.5, 0.5);
+ self.shoot = function () {
+ // Hero shoot logic
+ };
+ self.update = function () {
+ // Hero update logic
+ };
});
// Enemy class
var Enemy = Container.expand(function () {
- var self = Container.call(this);
- var enemyGraphics = self.createAsset('enemy', 'Enemy character', 0.5, 0.5);
- self.speed = 2;
- self.move = function () {
- self.y += self.speed;
- };
+ var self = Container.call(this);
+ var enemyGraphics = self.createAsset('enemy', 'Enemy character', 0.5, 0.5);
+ self.speed = 2;
+ self.move = function () {
+ self.y += self.speed;
+ };
});
-// Bullet class
-var Bullet = Container.expand(function () {
- var self = Container.call(this);
- var bulletGraphics = self.createAsset('bullet', 'Bullet Graphics', 0.5, 0.5);
- self.speed = -10;
- self.move = function () {
- self.y += self.speed;
- };
+// BlueSquareBullet class
+var BlueSquareBullet = Container.expand(function () {
+ var self = Container.call(this);
+ var bulletGraphics = self.createAsset('blueSquare', 'Blue Square Bullet', 0.5, 0.5);
+ bulletGraphics.tint = 0x0000ff; // Tint the bullet blue
+ self.speed = -10;
+ self.move = function () {
+ self.y += self.speed;
+ };
});
-/****
+/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000000 // Init game with black background
+ backgroundColor: 0x000000 // Init game with black background
});
-/****
+/****
* Game Code
****/
// Initialize important asset arrays
var bullets = [];
@@ -49,85 +50,90 @@
hero.x = 2048 / 2;
hero.y = 2732 - 200; // Position hero near the bottom of the screen
// Score display
var scoreTxt = new Text2('0', {
- size: 150,
- fill: "#ffffff"
+ size: 150,
+ fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Game logic
var isGameOver = false;
var score = 0;
// Event listeners
game.on('down', function (obj) {
- var touchPos = obj.event.getLocalPosition(game);
- hero.x = touchPos.x;
- hero.shoot();
+ var touchPos = obj.event.getLocalPosition(game);
+ hero.x = touchPos.x;
+ hero.shoot();
});
+window.addEventListener('keydown', function (e) {
+ if (e.key === 'k' || e.key === 'K') {
+ hero.shoot();
+ }
+});
LK.on('tick', function () {
- if (!isGameOver) {
- // Update hero
- hero.update();
- // Move and check bullets
- for (var b = bullets.length - 1; b >= 0; b--) {
- bullets[b].move();
- if (bullets[b].y < 0) {
- bullets[b].destroy();
- bullets.splice(b, 1);
- } else {
- // Check for bullet-enemy collisions
- for (var e = enemies.length - 1; e >= 0; e--) {
- if (bullets[b].intersects(enemies[e])) {
- score += 1;
- scoreTxt.setText(score.toString());
- enemies[e].destroy();
- enemies.splice(e, 1);
- bullets[b].destroy();
- bullets.splice(b, 1);
- break;
- }
- }
- }
- }
- // Move and check enemies
- for (var e = enemies.length - 1; e >= 0; e--) {
- enemies[e].move();
- if (enemies[e].y > 2732) {
- enemies[e].destroy();
- enemies.splice(e, 1);
- } else if (enemies[e].intersects(hero)) {
- isGameOver = true;
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
- }
- // Spawn enemies
- if (LK.ticks % 120 == 0) {
- var newEnemy = new Enemy();
- newEnemy.x = Math.random() * 2048;
- newEnemy.y = -50;
- enemies.push(newEnemy);
- game.addChild(newEnemy);
- }
- }
+ if (!isGameOver) {
+ // Update hero
+ hero.update();
+ // Move and check bullets
+ for (var b = bullets.length - 1; b >= 0; b--) {
+ bullets[b].move();
+ if (bullets[b].y < 0) {
+ bullets[b].destroy();
+ bullets.splice(b, 1);
+ } else {
+ // Check for bullet-enemy collisions
+ for (var e = enemies.length - 1; e >= 0; e--) {
+ if (bullets[b].intersects(enemies[e])) {
+ score += 1;
+ scoreTxt.setText(score.toString());
+ enemies[e].destroy();
+ enemies.splice(e, 1);
+ bullets[b].destroy();
+ bullets.splice(b, 1);
+ break;
+ }
+ }
+ }
+ }
+ // Move and check enemies
+ for (var e = enemies.length - 1; e >= 0; e--) {
+ enemies[e].move();
+ if (enemies[e].y > 2732) {
+ enemies[e].destroy();
+ enemies.splice(e, 1);
+ } else if (enemies[e].intersects(hero)) {
+ isGameOver = true;
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ }
+ }
+ // Spawn enemies
+ if (LK.ticks % 120 == 0) {
+ var newEnemy = new Enemy();
+ newEnemy.x = Math.random() * 2048;
+ newEnemy.y = -50;
+ enemies.push(newEnemy);
+ game.addChild(newEnemy);
+ }
+ }
});
// Hero shoot method
Hero.prototype.shoot = function () {
- var newBullet = new Bullet();
- newBullet.x = this.x;
- newBullet.y = this.y;
- bullets.push(newBullet);
- game.addChild(newBullet);
+ var newBullet = new BlueSquareBullet();
+ newBullet.x = this.x;
+ newBullet.y = this.y;
+ bullets.push(newBullet);
+ game.addChild(newBullet);
};
// Hero update method
Hero.prototype.update = function () {
- // Add hero update logic if needed
+ // Add hero update logic if needed
};
// Enemy move method
Enemy.prototype.move = function () {
- this.y += this.speed;
+ this.y += this.speed;
};
// Bullet move method
Bullet.prototype.move = function () {
- this.y += this.speed;
+ this.y += this.speed;
};
\ No newline at end of file