简体   繁体   English

在Phaser.IO中沿预定义路径移动精灵

[英]Moving a sprite along a pre-defined path in Phaser.IO

我有一个精灵和它的路径(路径= [[1,1],[1,2],[1,3]),使用game.physics进行它的最佳做法是什么,而不是简单的chage x,y值?

Presuming you have enabled physics and have already assigned each of the coordinates in your path to regions of your stage. 假设您已经启用了物理,并且已经将路径中的每个坐标分配给舞台的区域。

Moving in straight lines 直线移动

I would suggest Physics.Arcade.movetoXY() . 我建议使用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)
);

If your sprite is drifting past the target and not stopping, be sure to include something like this in your main update() loop: 如果您的精灵漂移过目标并且没有停止,请务必在主update()循环中包含类似的内容:

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

As your example is only moving in straight line this method will work well. 由于你的例子只是直线移动,这种方法效果很好。 You can just use the xy position of your final tile and set your sprite moving. 您可以使用最终图块的xy位置并设置精灵移动。 This method does not account for drag, acceleration and a few other variables your game might have. 此方法不考虑您的游戏可能具有的阻力,加速度和一些其他变量。

A lot of what to recommend depends on your use case. 很多推荐的内容取决于您的使用案例。

Patroling a tile path 巡逻瓷砖路径

If you wanted the sprite to 'patrol' along a path then you would use a tween, as this offers the best performance. 如果你想让精灵沿路径“巡逻”,那么你就可以使用补间,因为这样可以提供最佳性能。

Tile-based complicated movement 基于瓷砖的复杂运动

If your sprite was following a more complicated path such as avoiding obsticles or following another game object, you would use a Phaser.Timer (for Pokemen-style, tile-by-tile movement) or your game's update() loop (for smooth movement), to run a function that calculates which direction to move in and then executes that move using a Phaser.Arcade.Physics method (moveToXY, accelerateToXy etc) to move. 如果您的精灵遵循更复杂的路径,例如避免使用obsticles或跟随另一个游戏对象,您将使用Phaser.Timer (用于Pokemen风格,逐个tile的移动)或游戏的update()循环(用于平滑移动) ),运行一个函数,计算要移入的方向,然后使用Phaser.Arcade.Physics方法(moveToXY,accelerateToXy等)移动来执行该移动。

The best way to do this is with an A* path-finding algorithm , which work out a path for your sprite, taking into account obsticles, dead ends and difficult terrain to give the quickest path. 最好的方法是使用A *路径寻找算法 ,它为你的精灵计算出一条路径,考虑到obsticles,死角和困难的地形来提供最快的路径。

rot.js is a library of tile-based methods, including an A* path finder. rot.js是一个基于图块的方法库,包括一个A *路径查找器。

Tile-based character movement 基于平铺的角色运动

In tile-based movement with obstacle collisions be careful - the arcade collisions in Phaser fire upon contact with the target (including corner pixels), and this can make moving between obstacles or moving past openings difficult. 在具有障碍物碰撞的基于瓦片的运动中要小心 - 在与目标(包括角落像素)接触时,Phaser中的街机碰撞会发生火灾,这会使障碍物之间的移动或移动通过开口变得困难。

You can find that simply increasing velocity in a direction does not work any more. 你会发现简单地增加一个方向的速度不再起作用。

Solve this by making your character's arcade body smaller than a tile by a few pixels with sprite.body.setBounds() , and use the excellent Math.snapTo() to keep your sprite in the center of a tile. 通过sprite.body.setBounds()使你的角色的街机体比瓷砖小几个像素来解决这个问题,并使用优秀的Math.snapTo()将你的精灵保持在瓷砖的中心。

You can adapt this for all four directions, and add to your update() loop for smooth tile based character movement. 您可以对所有四个方向进行调整,并添加到您的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
) };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM