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

I wanted it to be like the blue line, and not as green.

cb1On.png

 

 

I tried with this plugin for phaser, editing to function as the blue line? I tried using the function astar.disableDiagonals(); and astar.enableDiagonals();..but did not work.

Link to comment
Share on other sites

That is a completely different problem.

That is telling you that the tileset function you are trying to call does not exist.

If you are loading a sprite sheet to use as a tileset, you probably want

game.load.spritesheet('img1', 'resources/1.png', 32, 32);
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...