/**** * Classes ****/ //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Arrow class for shooting arrows in 360 degrees var Arrow = Container.expand(function () { var self = Container.call(this); var arrowGraphics = self.attachAsset('arrow', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.direction = 0; // Direction in radians self.update = function () { // Update the position of the arrow every frame self.x += self.speed * Math.cos(self.direction); self.y += self.speed * Math.sin(self.direction); }; }); // Slime class for enemy slimes var Slime = Container.expand(function () { var self = Container.call(this); var slimeGraphics = self.attachAsset('slime', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 1; self.update = function () { var dx = hero.x - self.x; var dy = hero.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); self.x += dx / distance * self.speed; self.y += dy / distance * self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var hero = game.addChild(new Container()); var heroGraphics = hero.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); hero.x = 2048 / 2; hero.y = 2732 / 2; var arrows = []; var slimes = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); function spawnSlimes() { for (var i = 0; i < 5; i++) { var slime = new Slime(); slime.x = Math.random() * 2048; slime.y = Math.random() * 2732; slimes.push(slime); game.addChild(slime); } } game.down = function (x, y, obj) { for (var angle = 0; angle < Math.PI * 2; angle += Math.PI / 4) { var arrow = new Arrow(); arrow.x = hero.x; arrow.y = hero.y; arrow.direction = angle; arrows.push(arrow); game.addChild(arrow); } }; game.update = function () { for (var i = arrows.length - 1; i >= 0; i--) { var arrow = arrows[i]; arrow.update(); if (arrow.x < 0 || arrow.x > 2048 || arrow.y < 0 || arrow.y > 2732) { arrow.destroy(); arrows.splice(i, 1); } } for (var i = slimes.length - 1; i >= 0; i--) { var slime = slimes[i]; slime.update(); if (hero.intersects(slime)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } for (var j = arrows.length - 1; j >= 0; j--) { var arrow = arrows[j]; if (arrow.intersects(slime)) { arrow.destroy(); arrows.splice(j, 1); slime.destroy(); slimes.splice(i, 1); break; } } } if (slimes.length === 0) { score++; scoreTxt.setText(score); spawnSlimes(); } }; spawnSlimes();
===================================================================
--- original.js
+++ change.js
@@ -12,10 +12,9 @@
});
self.speed = 10;
self.direction = 0; // Direction in radians
self.update = function () {
- // Update the direction of the arrow every frame
- self.direction += Math.PI / 30; // Change this value to make the arrow rotate faster or slower
+ // Update the position of the arrow every frame
self.x += self.speed * Math.cos(self.direction);
self.y += self.speed * Math.sin(self.direction);
};
});