s4m_ur4i Posted December 13, 2015 Share Posted December 13, 2015 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 More sharing options...
s4m_ur4i Posted December 29, 2015 Author Share Posted December 29, 2015 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 More sharing options...
Recommended Posts