User prompt
upgrader lar her 2 kill alınca enemy nin öldüğü yerde spawn olsun.
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'to')' in or related to this line: 'tween(asset).to({' Line Number: 197 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'TypeError: tween.to is not a function' in or related to this line: 'tween.to(asset, {' Line Number: 197
User prompt
kılıç çok daha hızlı dönsün ve yerde random timelarda 0-30sn upgrader lar çıksın. bunlar kılıcın hızını arttırma, bir kılıç daha ekleme gibi
User prompt
kılıç tüm çevremde dönsün
Code edit (1 edits merged)
Please save this source code
User prompt
Kılıçlı Savaşçı: Düşman Avı
Initial prompt
bana kılıçlı bir dövüş oyunu yap. düşmanlar olsun. onların radarına girersem beni kovalasın. vurarak onları öldürebileyim.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// --- Enemy Class ---
var Enemy = Container.expand(function () {
var self = Container.call(this);
// Enemy asset: red ellipse
var enemyAsset = self.attachAsset('enemy', {
width: 100,
height: 100,
color: 0xd83318,
shape: 'ellipse',
anchorX: 0.5,
anchorY: 0.5
});
self.radius = 50;
self.speed = 4 + Math.random() * 2; // randomize speed a bit
self.state = 'idle'; // 'idle' or 'chase'
self.radarRadius = 400; // detection radius
self.target = null; // player
// For hit flash
self.flashTicks = 0;
self.update = function () {
if (!self.target) return;
var dx = self.target.x - self.x;
var dy = self.target.y - self.y;
var dist = Math.sqrt(dx * dx + dy * dy);
// Radar detection
if (self.state === 'idle' && dist < self.radarRadius) {
self.state = 'chase';
}
// Move if chasing
if (self.state === 'chase') {
if (dist > 5) {
var moveX = dx / dist * self.speed;
var moveY = dy / dist * self.speed;
self.x += moveX;
self.y += moveY;
}
}
// Flash on hit
if (self.flashTicks > 0) {
self.flashTicks--;
enemyAsset.tint = 0xffff00;
} else {
enemyAsset.tint = 0xd83318;
}
};
// Called when hit by sword
self.hit = function () {
self.flashTicks = 6;
};
return self;
});
// --- Player Class ---
var Player = Container.expand(function () {
var self = Container.call(this);
// Player asset: a colored ellipse (body)
var body = self.attachAsset('player', {
width: 120,
height: 120,
color: 0x2e86de,
shape: 'ellipse',
anchorX: 0.5,
anchorY: 0.5
});
// Sword child
self.sword = new Sword();
self.addChild(self.sword);
self.sword.x = 0;
self.sword.y = 0;
self.radius = 60; // for collision
// For dragging
self.isDragging = false;
// For invulnerability after hit
self.invulnTicks = 0;
// Called every tick
self.update = function () {
if (self.invulnTicks > 0) {
self.invulnTicks--;
body.alpha = LK.ticks % 10 < 5 ? 0.5 : 1;
} else {
body.alpha = 1;
}
};
// Start sword swing
self.attack = function (onFinish) {
self.sword.swing(onFinish);
};
return self;
});
// --- Sword Class ---
var Sword = Container.expand(function () {
var self = Container.call(this);
// Sword asset: a long, thin rectangle (box)
var swordAsset = self.attachAsset('sword', {
width: 40,
height: 220,
color: 0xffffff,
shape: 'box',
anchorX: 0.5,
anchorY: 0.9 // handle is at bottom
});
self.swinging = false;
self.swingAngle = 0; // in radians
// Called every tick
self.update = function () {
// If swinging, animate the sword
if (self.swinging) {
self.rotation = self.swingAngle;
}
};
// Start a sword swing animation
self.swing = function (_onFinish) {
if (self.swinging) return;
self.swinging = true;
self.swingAngle = -Math.PI / 2.5; // Start angle (left)
self.rotation = self.swingAngle;
tween(self, {
swingAngle: Math.PI / 2.5
}, {
duration: 220,
easing: tween.cubicOut,
onFinish: function onFinish() {
self.swinging = false;
self.swingAngle = 0;
self.rotation = 0;
if (_onFinish) _onFinish();
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x181818
});
/****
* Game Code
****/
// --- Game Variables ---
var player;
var enemies = [];
var maxEnemies = 6;
var spawnAreaMargin = 200;
var score = 0;
var lives = 3;
var scoreTxt, livesTxt;
var dragNode = null;
var lastTouch = {
x: 0,
y: 0
};
var swordHitCooldown = 0;
// --- UI Setup ---
scoreTxt = new Text2('Skor: 0', {
size: 100,
fill: "#fff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
livesTxt = new Text2('Can: 3', {
size: 80,
fill: "#fff"
});
livesTxt.anchor.set(1, 0);
livesTxt.x = -40; // right margin
LK.gui.topRight.addChild(livesTxt);
// --- Player Setup ---
player = new Player();
player.x = 2048 / 2;
player.y = 2732 * 0.7;
game.addChild(player);
// --- Enemy Spawning ---
function spawnEnemy() {
var enemy = new Enemy();
enemy.target = player;
// Spawn at random edge
var edge = Math.floor(Math.random() * 4);
var x, y;
if (edge === 0) {
// top
x = spawnAreaMargin + Math.random() * (2048 - 2 * spawnAreaMargin);
y = spawnAreaMargin;
} else if (edge === 1) {
// bottom
x = spawnAreaMargin + Math.random() * (2048 - 2 * spawnAreaMargin);
y = 2732 - spawnAreaMargin;
} else if (edge === 2) {
// left
x = spawnAreaMargin;
y = spawnAreaMargin + Math.random() * (2732 - 2 * spawnAreaMargin);
} else {
// right
x = 2048 - spawnAreaMargin;
y = spawnAreaMargin + Math.random() * (2732 - 2 * spawnAreaMargin);
}
enemy.x = x;
enemy.y = y;
enemies.push(enemy);
game.addChild(enemy);
}
// Initial enemies
for (var i = 0; i < maxEnemies; i++) {
spawnEnemy();
}
// --- Touch/Drag Controls ---
game.down = function (x, y, obj) {
// Only drag if touch is on player
var dx = x - player.x;
var dy = y - player.y;
if (dx * dx + dy * dy < player.radius * player.radius) {
dragNode = player;
lastTouch.x = x;
lastTouch.y = y;
}
};
game.move = function (x, y, obj) {
if (dragNode === player) {
// Clamp to game area
var nx = Math.max(player.radius, Math.min(2048 - player.radius, x));
var ny = Math.max(player.radius, Math.min(2732 - player.radius, y));
player.x = nx;
player.y = ny;
lastTouch.x = x;
lastTouch.y = y;
}
};
game.up = function (x, y, obj) {
dragNode = null;
};
// --- Sword Attack (Tap anywhere except player) ---
game.tapCooldown = 0;
game.downSword = function (x, y, obj) {
// If not dragging player, swing sword
var dx = x - player.x;
var dy = y - player.y;
if (dx * dx + dy * dy > player.radius * player.radius) {
if (!player.sword.swinging && swordHitCooldown === 0) {
player.attack();
swordHitCooldown = 12; // cooldown in ticks
}
}
};
// Listen for down events for sword attack
game.on('down', function (x, y, obj) {
game.downSword(x, y, obj);
});
// --- Game Update Loop ---
game.update = function () {
player.update();
for (var i = enemies.length - 1; i >= 0; i--) {
var enemy = enemies[i];
enemy.update();
// Check collision: enemy with player
var dx = enemy.x - player.x;
var dy = enemy.y - player.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < enemy.radius + player.radius - 10) {
// Only if not invulnerable
if (player.invulnTicks === 0) {
lives--;
livesTxt.setText('Can: ' + lives);
player.invulnTicks = 60;
LK.effects.flashObject(player, 0xff0000, 600);
if (lives <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
}
}
// Check collision: enemy with sword (only if sword is swinging)
if (player.sword.swinging) {
// Sword tip position (relative to player)
var swordLen = player.sword.height * 0.9;
var angle = player.sword.rotation;
var tipX = player.x + Math.sin(angle) * swordLen;
var tipY = player.y - Math.cos(angle) * swordLen;
var sdx = enemy.x - tipX;
var sdy = enemy.y - tipY;
var sdist = Math.sqrt(sdx * sdx + sdy * sdy);
if (sdist < enemy.radius + 30) {
// Enemy hit!
score++;
scoreTxt.setText('Skor: ' + score);
enemy.hit();
LK.effects.flashObject(enemy, 0xffff00, 200);
enemy.destroy();
enemies.splice(i, 1);
// Spawn new enemy
LK.setTimeout(function () {
if (enemies.length < maxEnemies) spawnEnemy();
}, 400);
// Win condition
if (score >= 30) {
LK.showYouWin();
return;
}
}
}
}
// Sword cooldown
if (swordHitCooldown > 0) swordHitCooldown--;
// Keep enemy count
if (enemies.length < maxEnemies && LK.ticks % 90 === 0) {
spawnEnemy();
}
};
// --- Prevent UI Overlap (top left 100x100) ---
player.x = Math.max(player.x, 160);
player.y = Math.max(player.y, 160); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,321 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+// --- Enemy Class ---
+var Enemy = Container.expand(function () {
+ var self = Container.call(this);
+ // Enemy asset: red ellipse
+ var enemyAsset = self.attachAsset('enemy', {
+ width: 100,
+ height: 100,
+ color: 0xd83318,
+ shape: 'ellipse',
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.radius = 50;
+ self.speed = 4 + Math.random() * 2; // randomize speed a bit
+ self.state = 'idle'; // 'idle' or 'chase'
+ self.radarRadius = 400; // detection radius
+ self.target = null; // player
+ // For hit flash
+ self.flashTicks = 0;
+ self.update = function () {
+ if (!self.target) return;
+ var dx = self.target.x - self.x;
+ var dy = self.target.y - self.y;
+ var dist = Math.sqrt(dx * dx + dy * dy);
+ // Radar detection
+ if (self.state === 'idle' && dist < self.radarRadius) {
+ self.state = 'chase';
+ }
+ // Move if chasing
+ if (self.state === 'chase') {
+ if (dist > 5) {
+ var moveX = dx / dist * self.speed;
+ var moveY = dy / dist * self.speed;
+ self.x += moveX;
+ self.y += moveY;
+ }
+ }
+ // Flash on hit
+ if (self.flashTicks > 0) {
+ self.flashTicks--;
+ enemyAsset.tint = 0xffff00;
+ } else {
+ enemyAsset.tint = 0xd83318;
+ }
+ };
+ // Called when hit by sword
+ self.hit = function () {
+ self.flashTicks = 6;
+ };
+ return self;
+});
+// --- Player Class ---
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ // Player asset: a colored ellipse (body)
+ var body = self.attachAsset('player', {
+ width: 120,
+ height: 120,
+ color: 0x2e86de,
+ shape: 'ellipse',
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Sword child
+ self.sword = new Sword();
+ self.addChild(self.sword);
+ self.sword.x = 0;
+ self.sword.y = 0;
+ self.radius = 60; // for collision
+ // For dragging
+ self.isDragging = false;
+ // For invulnerability after hit
+ self.invulnTicks = 0;
+ // Called every tick
+ self.update = function () {
+ if (self.invulnTicks > 0) {
+ self.invulnTicks--;
+ body.alpha = LK.ticks % 10 < 5 ? 0.5 : 1;
+ } else {
+ body.alpha = 1;
+ }
+ };
+ // Start sword swing
+ self.attack = function (onFinish) {
+ self.sword.swing(onFinish);
+ };
+ return self;
+});
+// --- Sword Class ---
+var Sword = Container.expand(function () {
+ var self = Container.call(this);
+ // Sword asset: a long, thin rectangle (box)
+ var swordAsset = self.attachAsset('sword', {
+ width: 40,
+ height: 220,
+ color: 0xffffff,
+ shape: 'box',
+ anchorX: 0.5,
+ anchorY: 0.9 // handle is at bottom
+ });
+ self.swinging = false;
+ self.swingAngle = 0; // in radians
+ // Called every tick
+ self.update = function () {
+ // If swinging, animate the sword
+ if (self.swinging) {
+ self.rotation = self.swingAngle;
+ }
+ };
+ // Start a sword swing animation
+ self.swing = function (_onFinish) {
+ if (self.swinging) return;
+ self.swinging = true;
+ self.swingAngle = -Math.PI / 2.5; // Start angle (left)
+ self.rotation = self.swingAngle;
+ tween(self, {
+ swingAngle: Math.PI / 2.5
+ }, {
+ duration: 220,
+ easing: tween.cubicOut,
+ onFinish: function onFinish() {
+ self.swinging = false;
+ self.swingAngle = 0;
+ self.rotation = 0;
+ if (_onFinish) _onFinish();
+ }
+ });
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x181818
+});
+
+/****
+* Game Code
+****/
+// --- Game Variables ---
+var player;
+var enemies = [];
+var maxEnemies = 6;
+var spawnAreaMargin = 200;
+var score = 0;
+var lives = 3;
+var scoreTxt, livesTxt;
+var dragNode = null;
+var lastTouch = {
+ x: 0,
+ y: 0
+};
+var swordHitCooldown = 0;
+// --- UI Setup ---
+scoreTxt = new Text2('Skor: 0', {
+ size: 100,
+ fill: "#fff"
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+livesTxt = new Text2('Can: 3', {
+ size: 80,
+ fill: "#fff"
+});
+livesTxt.anchor.set(1, 0);
+livesTxt.x = -40; // right margin
+LK.gui.topRight.addChild(livesTxt);
+// --- Player Setup ---
+player = new Player();
+player.x = 2048 / 2;
+player.y = 2732 * 0.7;
+game.addChild(player);
+// --- Enemy Spawning ---
+function spawnEnemy() {
+ var enemy = new Enemy();
+ enemy.target = player;
+ // Spawn at random edge
+ var edge = Math.floor(Math.random() * 4);
+ var x, y;
+ if (edge === 0) {
+ // top
+ x = spawnAreaMargin + Math.random() * (2048 - 2 * spawnAreaMargin);
+ y = spawnAreaMargin;
+ } else if (edge === 1) {
+ // bottom
+ x = spawnAreaMargin + Math.random() * (2048 - 2 * spawnAreaMargin);
+ y = 2732 - spawnAreaMargin;
+ } else if (edge === 2) {
+ // left
+ x = spawnAreaMargin;
+ y = spawnAreaMargin + Math.random() * (2732 - 2 * spawnAreaMargin);
+ } else {
+ // right
+ x = 2048 - spawnAreaMargin;
+ y = spawnAreaMargin + Math.random() * (2732 - 2 * spawnAreaMargin);
+ }
+ enemy.x = x;
+ enemy.y = y;
+ enemies.push(enemy);
+ game.addChild(enemy);
+}
+// Initial enemies
+for (var i = 0; i < maxEnemies; i++) {
+ spawnEnemy();
+}
+// --- Touch/Drag Controls ---
+game.down = function (x, y, obj) {
+ // Only drag if touch is on player
+ var dx = x - player.x;
+ var dy = y - player.y;
+ if (dx * dx + dy * dy < player.radius * player.radius) {
+ dragNode = player;
+ lastTouch.x = x;
+ lastTouch.y = y;
+ }
+};
+game.move = function (x, y, obj) {
+ if (dragNode === player) {
+ // Clamp to game area
+ var nx = Math.max(player.radius, Math.min(2048 - player.radius, x));
+ var ny = Math.max(player.radius, Math.min(2732 - player.radius, y));
+ player.x = nx;
+ player.y = ny;
+ lastTouch.x = x;
+ lastTouch.y = y;
+ }
+};
+game.up = function (x, y, obj) {
+ dragNode = null;
+};
+// --- Sword Attack (Tap anywhere except player) ---
+game.tapCooldown = 0;
+game.downSword = function (x, y, obj) {
+ // If not dragging player, swing sword
+ var dx = x - player.x;
+ var dy = y - player.y;
+ if (dx * dx + dy * dy > player.radius * player.radius) {
+ if (!player.sword.swinging && swordHitCooldown === 0) {
+ player.attack();
+ swordHitCooldown = 12; // cooldown in ticks
+ }
+ }
+};
+// Listen for down events for sword attack
+game.on('down', function (x, y, obj) {
+ game.downSword(x, y, obj);
+});
+// --- Game Update Loop ---
+game.update = function () {
+ player.update();
+ for (var i = enemies.length - 1; i >= 0; i--) {
+ var enemy = enemies[i];
+ enemy.update();
+ // Check collision: enemy with player
+ var dx = enemy.x - player.x;
+ var dy = enemy.y - player.y;
+ var dist = Math.sqrt(dx * dx + dy * dy);
+ if (dist < enemy.radius + player.radius - 10) {
+ // Only if not invulnerable
+ if (player.invulnTicks === 0) {
+ lives--;
+ livesTxt.setText('Can: ' + lives);
+ player.invulnTicks = 60;
+ LK.effects.flashObject(player, 0xff0000, 600);
+ if (lives <= 0) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ return;
+ }
+ }
+ }
+ // Check collision: enemy with sword (only if sword is swinging)
+ if (player.sword.swinging) {
+ // Sword tip position (relative to player)
+ var swordLen = player.sword.height * 0.9;
+ var angle = player.sword.rotation;
+ var tipX = player.x + Math.sin(angle) * swordLen;
+ var tipY = player.y - Math.cos(angle) * swordLen;
+ var sdx = enemy.x - tipX;
+ var sdy = enemy.y - tipY;
+ var sdist = Math.sqrt(sdx * sdx + sdy * sdy);
+ if (sdist < enemy.radius + 30) {
+ // Enemy hit!
+ score++;
+ scoreTxt.setText('Skor: ' + score);
+ enemy.hit();
+ LK.effects.flashObject(enemy, 0xffff00, 200);
+ enemy.destroy();
+ enemies.splice(i, 1);
+ // Spawn new enemy
+ LK.setTimeout(function () {
+ if (enemies.length < maxEnemies) spawnEnemy();
+ }, 400);
+ // Win condition
+ if (score >= 30) {
+ LK.showYouWin();
+ return;
+ }
+ }
+ }
+ }
+ // Sword cooldown
+ if (swordHitCooldown > 0) swordHitCooldown--;
+ // Keep enemy count
+ if (enemies.length < maxEnemies && LK.ticks % 90 === 0) {
+ spawnEnemy();
+ }
+};
+// --- Prevent UI Overlap (top left 100x100) ---
+player.x = Math.max(player.x, 160);
+player.y = Math.max(player.y, 160);
\ No newline at end of file