Jump to content


Recommended Posts

I use easystarJS and the PhaserPlugin to generate coordinates for pathfinding. This works well and I've got an array called "Path" which contains every x/y coordinates that a sprite should be moved to. The pathfinding "Event" fires 1 time when clicking on a map layer.

some code:

this.findPathTo = function(tilex, tiley) {    pathfinder.setCallbackFunction(function(path) { //callback with path      var path = path; //path array with ever x / y coordinate for every tile to walk over      for (var i = 0, ilen = path.length; i < ilen; i++){        var num = i;/*        setTimeout(function(){// This is a bad idear, what to put here?          console.log(path[num]);          reference.DBugger.x = path[num].x * 20;          reference.DBugger.y = path[num].y * 20;                  }, 100);*/      }    });    pathfinder.preparePathCalculation([0,0], [tilex,tiley]); //pathfinding functions    pathfinder.calculatePath(); //pathfinding functions  }

What I want to achieve is that the Sprite => reference.DBugger (Sprite for testing) moves through all the coordinates in that array piece by piece. If I uncomment that function atop, the sprite is already at the position were I have clicked. The best way would be some function that checks when the sprite has reached path[0] coordinates and then applies path[1] coordination and so on.

any suggestions? 

kind regards

Link to comment
Share on other sites

  • 3 weeks later...

my solution if someone is interested in it:

Worker.prototype.moveTo = function( target ){  if(this.ismoving === true){      this.tween.stop();  }  var path = _this.path.find( this, target ),      i = 0,      ilen = path.length;  function moveObject( object ){    if(path.length > 1){      object.ismoving = true;      var StepX = path[i][0] || false,          StepY = path[i][1] || false;      object.tween = game.add.tween( object ).to({ x: StepX, y: StepY}, 150).start();      object.tween.onComplete.add(function(){        i++;        if(i < ilen){          moveObject( object );        }else{          return false;        }      });    }else{      //@TODO add emoticon - no path (!)      this.ismoving = false;    }  }  moveObject( this );};
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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