User prompt
Nereye ateş edersek cöp adam oraya baksın ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Adamlar gelmesin bı yerde rasgele doğsun ama ölünce başkası doğsun teker teker sonra hızlı canımız olsun 10 ok deyerse ölelim adamlarda bize ok atsın
User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'toGlobal')' in or related to this line: 'var gamePos = game.toLocal(obj.parent.toGlobal({' Line Number: 116
User prompt
Ekrana basıyorum ama ok gelmiyor
User prompt
Hata var
User prompt
Assagida tuş oosun ona basınca otomatik savaş olsun
User prompt
Otomatik savaş olsun
User prompt
Ok atarken enerji artmasin durunca artsin
User prompt
Enerji her saniye artsın ve her okta 2 enerji harcasin
User prompt
Enerji olsun enerji 100 olsun her ok firlattiginda azalsın
Code edit (1 edits merged)
Please save this source code
User prompt
Arrow Defense
Initial prompt
Bana oyun yap sol da duran önüne çöp adamlar çıkan onlara oklarla vururup gelisien
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Archer = Container.expand(function () {
var self = Container.call(this);
var archerGraphics = self.attachAsset('archer', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
var Arrow = Container.expand(function () {
var self = Container.call(this);
var arrowGraphics = self.attachAsset('arrow', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
};
return self;
});
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.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var archer = game.addChild(new Archer());
archer.x = 150;
archer.y = 2732 / 2;
var arrows = [];
var enemies = [];
var enemySpawnTimer = 0;
var enemySpawnRate = 120; // frames between spawns
var energy = 100;
var energyRegenTimer = 0;
var isShooting = false;
var shootingTimer = 0;
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.setText(LK.getScore());
var energyTxt = new Text2('Energy: 100', {
size: 80,
fill: 0x00ff00
});
energyTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(energyTxt);
energyTxt.x = -20;
energyTxt.y = 20;
var autoShootTimer = 0;
var autoBattleActive = false;
// Create auto battle button
var autoBattleButton = new Text2('AUTO BATTLE', {
size: 80,
fill: 0xff0000
});
autoBattleButton.anchor.set(0.5, 1);
LK.gui.bottom.addChild(autoBattleButton);
autoBattleButton.y = -50;
// Button click handler
autoBattleButton.down = function (x, y, obj) {
autoBattleActive = !autoBattleActive;
if (autoBattleActive) {
autoBattleButton.setText('STOP AUTO');
autoBattleButton.tint = 0x00ff00; // Green when active
} else {
autoBattleButton.setText('AUTO BATTLE');
autoBattleButton.tint = 0xff0000; // Red when inactive
}
};
function shootArrowAtTarget(targetX, targetY) {
// Check if player has enough energy
if (energy <= 0) {
return;
}
// Calculate direction from archer to target point
var dirX = targetX - archer.x;
var dirY = targetY - archer.y;
var distance = Math.sqrt(dirX * dirX + dirY * dirY);
// Normalize direction
var normalX = dirX / distance;
var normalY = dirY / distance;
// Create arrow
var newArrow = new Arrow();
newArrow.x = archer.x;
newArrow.y = archer.y;
newArrow.speedX = normalX * 15;
newArrow.speedY = normalY * 15;
// Rotate arrow to face direction
newArrow.rotation = Math.atan2(normalY, normalX);
arrows.push(newArrow);
game.addChild(newArrow);
LK.getSound('shoot').play();
// Decrease energy
energy -= 2;
// Set shooting state
isShooting = true;
shootingTimer = 0;
// Update energy display
energyTxt.setText('Energy: ' + energy);
// Change color based on energy level
if (energy <= 20) {
energyTxt.tint = 0xff0000; // Red when low
} else if (energy <= 50) {
energyTxt.tint = 0xffff00; // Yellow when medium
} else {
energyTxt.tint = 0x00ff00; // Green when high
}
}
game.update = function () {
// Automatic shooting logic - only when auto battle is active
if (autoBattleActive) {
autoShootTimer++;
if (autoShootTimer >= 45 && energy > 0) {
// Shoot every 0.75 seconds if energy available
autoShootTimer = 0;
// Find nearest enemy
var nearestEnemy = null;
var nearestDistance = Infinity;
for (var e = 0; e < enemies.length; e++) {
var enemy = enemies[e];
var distanceToEnemy = Math.sqrt(Math.pow(enemy.x - archer.x, 2) + Math.pow(enemy.y - archer.y, 2));
if (distanceToEnemy < nearestDistance) {
nearestDistance = distanceToEnemy;
nearestEnemy = enemy;
}
}
// Shoot at nearest enemy if found
if (nearestEnemy) {
shootArrowAtTarget(nearestEnemy.x, nearestEnemy.y);
}
}
}
// Update shooting timer
if (isShooting) {
shootingTimer++;
if (shootingTimer >= 30) {
// Stop shooting state after 0.5 seconds
isShooting = false;
}
}
// Update energy regeneration only when not shooting (every 60 frames = 1 second at 60 FPS)
if (!isShooting) {
energyRegenTimer++;
if (energyRegenTimer >= 60) {
energyRegenTimer = 0;
if (energy < 100) {
energy += 1;
// Update energy display
energyTxt.setText('Energy: ' + energy);
// Change color based on energy level
if (energy <= 20) {
energyTxt.tint = 0xff0000; // Red when low
} else if (energy <= 50) {
energyTxt.tint = 0xffff00; // Yellow when medium
} else {
energyTxt.tint = 0x00ff00; // Green when high
}
}
}
} else {
energyRegenTimer = 0; // Reset regen timer when shooting
}
// Update enemy spawn timer
enemySpawnTimer++;
if (enemySpawnTimer >= enemySpawnRate) {
enemySpawnTimer = 0;
// Spawn new enemy
var newEnemy = new Enemy();
newEnemy.x = 2048 + 50;
newEnemy.y = Math.random() * (2732 - 200) + 100;
enemies.push(newEnemy);
game.addChild(newEnemy);
// Increase difficulty over time
if (enemySpawnRate > 30) {
enemySpawnRate -= 0.5;
}
}
// Update arrows
for (var i = arrows.length - 1; i >= 0; i--) {
var arrow = arrows[i];
// Remove arrows that go off screen
if (arrow.x > 2100 || arrow.x < -50 || arrow.y > 2800 || arrow.y < -50) {
arrow.destroy();
arrows.splice(i, 1);
continue;
}
// Check arrow-enemy collisions
for (var j = enemies.length - 1; j >= 0; j--) {
var enemy = enemies[j];
if (arrow.intersects(enemy)) {
// Hit enemy
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
LK.getSound('hit').play();
LK.effects.flashObject(enemy, 0xff0000, 200);
// Remove arrow and enemy
arrow.destroy();
arrows.splice(i, 1);
enemy.destroy();
enemies.splice(j, 1);
break;
}
}
}
// Update enemies and check if they reached the archer
for (var k = enemies.length - 1; k >= 0; k--) {
var enemy = enemies[k];
// Check if enemy reached archer
if (enemy.x <= archer.x + 50) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -77,8 +77,28 @@
LK.gui.topRight.addChild(energyTxt);
energyTxt.x = -20;
energyTxt.y = 20;
var autoShootTimer = 0;
+var autoBattleActive = false;
+// Create auto battle button
+var autoBattleButton = new Text2('AUTO BATTLE', {
+ size: 80,
+ fill: 0xff0000
+});
+autoBattleButton.anchor.set(0.5, 1);
+LK.gui.bottom.addChild(autoBattleButton);
+autoBattleButton.y = -50;
+// Button click handler
+autoBattleButton.down = function (x, y, obj) {
+ autoBattleActive = !autoBattleActive;
+ if (autoBattleActive) {
+ autoBattleButton.setText('STOP AUTO');
+ autoBattleButton.tint = 0x00ff00; // Green when active
+ } else {
+ autoBattleButton.setText('AUTO BATTLE');
+ autoBattleButton.tint = 0xff0000; // Red when inactive
+ }
+};
function shootArrowAtTarget(targetX, targetY) {
// Check if player has enough energy
if (energy <= 0) {
return;
@@ -117,28 +137,30 @@
energyTxt.tint = 0x00ff00; // Green when high
}
}
game.update = function () {
- // Automatic shooting logic
- autoShootTimer++;
- if (autoShootTimer >= 45 && energy > 0) {
- // Shoot every 0.75 seconds if energy available
- autoShootTimer = 0;
- // Find nearest enemy
- var nearestEnemy = null;
- var nearestDistance = Infinity;
- for (var e = 0; e < enemies.length; e++) {
- var enemy = enemies[e];
- var distanceToEnemy = Math.sqrt(Math.pow(enemy.x - archer.x, 2) + Math.pow(enemy.y - archer.y, 2));
- if (distanceToEnemy < nearestDistance) {
- nearestDistance = distanceToEnemy;
- nearestEnemy = enemy;
+ // Automatic shooting logic - only when auto battle is active
+ if (autoBattleActive) {
+ autoShootTimer++;
+ if (autoShootTimer >= 45 && energy > 0) {
+ // Shoot every 0.75 seconds if energy available
+ autoShootTimer = 0;
+ // Find nearest enemy
+ var nearestEnemy = null;
+ var nearestDistance = Infinity;
+ for (var e = 0; e < enemies.length; e++) {
+ var enemy = enemies[e];
+ var distanceToEnemy = Math.sqrt(Math.pow(enemy.x - archer.x, 2) + Math.pow(enemy.y - archer.y, 2));
+ if (distanceToEnemy < nearestDistance) {
+ nearestDistance = distanceToEnemy;
+ nearestEnemy = enemy;
+ }
}
+ // Shoot at nearest enemy if found
+ if (nearestEnemy) {
+ shootArrowAtTarget(nearestEnemy.x, nearestEnemy.y);
+ }
}
- // Shoot at nearest enemy if found
- if (nearestEnemy) {
- shootArrowAtTarget(nearestEnemy.x, nearestEnemy.y);
- }
}
// Update shooting timer
if (isShooting) {
shootingTimer++;