Jump to content

Movement pathfind


Lucas
 Share

Recommended Posts

How do the character's movement using pathfinding?

 

 

var tween;

 

function create(){

    game.input.onDown.add(moveSprite, this);

}

 

function moveSprite (pointer) {

    if (tween && tween.isRunning)
    {
        tween.stop();
    }
 
 
    var duration = (game.physics.distanceToPointer(localPlayer.sprite, pointer) / 200) * 1000;
 
    tween = game.add.tween(localPlayer.sprite).to({ x: pointer.x, y: pointer.y }, duration, Phaser.Easing.Linear.None, true);
}

 

 

Example:
430px-Pathfinding_A_Star.svg.png

Link to comment
Share on other sites

Finally, support for the new version: https://github.com/appsbu-de/phaser_plugin_pathfinding

thx

 

########

 

Now my avatar is teleporting map.How can I make it move following the correct coordinates?

function findPathTo(tilex, tiley) {   pathfinder.setCallbackFunction(function(path) {        path = path || [];        for(var i = 0, ilen = path.length; i < ilen; i++) {            localPlayer.sprite.x = path[i].x*32;            localPlayer.sprite.y = path[i].y*32;	}        blocked = false;    });    pathfinder.preparePathCalculation([0,0], [tilex,tiley]);    pathfinder.calculatePath();}

The lace (for) is making it run several times, hence teleportation.

 

 

######################

 

II did so:

function findPathTo(tilex, tiley) {   pathfinder.setCallbackFunction(function(path) {        path = path || [];		var i = 0;        var interval = setInterval(function () {		    if(i == path.length-1){ clearInterval(interval); blocked = false; }            localPlayer.sprite.x = path[i].x*32;            localPlayer.sprite.y = path[i].y*32;			++i;        }, speed/6);   });    pathfinder.preparePathCalculation([0,0], [tilex,tiley]);    pathfinder.calculatePath();}

And worked! Thx!

Link to comment
Share on other sites

  • 1 month later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...