/**** * Classes ****/ // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = Math.random() * 2 + 1; // Random speed between 1 and 3 self.move = function () { self.y += self.speed; }; }); // Assets will be automatically created based on usage in the code. // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.move = function (direction) { if (direction === 'left' && self.x > 0) { self.x -= self.speed; } else if (direction === 'right' && self.x < 2048) { self.x += self.speed; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ LK.on('tick', function () { // Calculate the distance between the player and the target position var dx = targetPosition.x - player.x; var dy = targetPosition.y - player.y; var distance = Math.sqrt(dx * dx + dy * dy); // If the player is not already at the target position if (distance > player.speed) { // Calculate the direction vector var directionX = dx / distance; var directionY = dy / distance; // Move the player towards the target position player.x += directionX * player.speed; player.y += directionY * player.speed; } }); var player = game.addChild(new Player()); player.x = 2048 / 2; // Center horizontally player.y = 2732 / 2; // Center vertically var targetPosition = { x: player.x, y: player.y }; // Store the target position for the player game.on('down', function (obj) { targetPosition = obj.event.getLocalPosition(game); // Set the target position to the clicked position });
===================================================================
--- original.js
+++ change.js
@@ -40,15 +40,29 @@
/****
* Game Code
****/
+LK.on('tick', function () {
+ // Calculate the distance between the player and the target position
+ var dx = targetPosition.x - player.x;
+ var dy = targetPosition.y - player.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ // If the player is not already at the target position
+ if (distance > player.speed) {
+ // Calculate the direction vector
+ var directionX = dx / distance;
+ var directionY = dy / distance;
+ // Move the player towards the target position
+ player.x += directionX * player.speed;
+ player.y += directionY * player.speed;
+ }
+});
var player = game.addChild(new Player());
player.x = 2048 / 2; // Center horizontally
player.y = 2732 / 2; // Center vertically
+var targetPosition = {
+ x: player.x,
+ y: player.y
+}; // Store the target position for the player
game.on('down', function (obj) {
- var pos = obj.event.getLocalPosition(game);
- if (pos.x < 1024) {
- player.move('left');
- } else {
- player.move('right');
- }
+ targetPosition = obj.event.getLocalPosition(game); // Set the target position to the clicked position
});
\ No newline at end of file
квадрат со злым лицом.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
нужно закрасить тень за квадратом
квадрат с испуганным лицом. Белый. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
квадрат со злым лицом. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.