Moving a sprite along a predefined path in Phaser.IO

I have a sprite and a path for it (path = [[1,1], [1,2], [1,3]), which is best suited for this using game.physics, and not just enter the x values, y?

+4
source share
1 answer

Assuming you turned on physics and already assigned each of the coordinates on your way to the regions of your scene.

Straight line movement

I would suggest Physics.Arcade.movetoXY () .

The function returns the angle to the target location if you need to rotate your sprite.

sprite.rotation = game.physics.arcade.moveToXY(
    sprite, 
    target.x, 
    target.y, 
    300 // speed, 
    500 // maxTimeToFinish(ms)
);

, - update():

player.body.velocity.x = 0;
player.body.velocity.y = 0;

, . xy . , , .

, , .

, "" , , .

, , , Phaser.Timer ( Pokemen, ) update() ), , , , Phaser.Arcade.Physics(moveToXY, accelerateToXy ..) .

- A *, , , , .

rot.js - , A * path finder.

- Phaser ( ), .

, .

, , sprite.body.setBounds(), Math.snapTo(), .

update() .

if (cursors.left.isDown) {
    game.physics.arcade.moveToXY(
    sprite, 
    sprite.body.x - 70, // target x position
    Phaser.Math.snapTo(sprite.body.y, 70), // keep y position the same as we are moving along x axis
    250 // velocity to move at
) };
+9

Source: https://habr.com/ru/post/1570037/


All Articles