## Recommended Posts

My enemy has a problem from the walls. How to do in order to do the players get?

does this:

I want to do:

`function update() {    game.physics.arcade.collide(monster, layer);    game.physics.arcade.moveToObject(monster1, player, 150);}`
##### Share on other sites

I can't figure how it could help me.

##### Share on other sites

I can't figure how it could help me.

So, your current code uses the moveToObject function, which tries to move the sprite along a straight line to the target sprite.  This doesn't account for things that are in the way, such as solid tiles.  What you need is a pathfinding algorithm to help your enemy find its way through the maze to the player.  As Skeptron recommended, A-star is your best bet and relatively easy to implement.  There's plenty of tutorials out there.  You can use the tiles of your tilemap as "grid points" for the algorithm to consider.

Example: here's my path finding algorithm from a recent game I did:

`/* * A helper class to do A-Star pathfinding to tiles */AStarSearch = function(entity, destinationTile, world){    this.world = world;    this.map = world.map;    this.destinationTile = destinationTile;    this.beginTile = this.map.getTile(Math.floor(entity.getX()/this.map.tileWidth), Math.floor(entity.getY()/this.map.tileHeight), 0);    return this;}AStarSearch.prototype = {    map: null,    beginTile: null,    destinationTile: null,    _open: [],    _closed: [],    getShortestPath: function(){        this._open = [];        this._closed = [];        var shortestPath = [];        this._iterativeSearch(this.beginTile, 0, shortestPath);        return shortestPath;    },    _iterativeSearch: function(current, d, shortestPath){        if(current === this.destinationTile){            this._backtrack(shortestPath);            return;        }        //push current tile to the closed list        this._closed.push(current);        //remove current tile from the open list        var i = this._open.indexOf(current);        if(i != -1){            this._open.splice(i, 1);        }        var currentCol = Math.floor(current.x/this.map.tileWidth);        var currentRow = Math.floor(current.y/this.map.tileHeight);        var north = this.map.getTileAbove(0, currentCol, currentRow);        var south = this.map.getTileBelow(0, currentCol, currentRow);        var east = this.map.getTileRight(0, currentCol, currentRow);        var west = this.map.getTileLeft(0, currentCol, currentRow);        if(north){            this._pushToOpen(north, current, this.destinationTile, d);        }        if(south){            this._pushToOpen(south, current, this.destinationTile, d);        }        if(east){            this._pushToOpen(east, current, this.destinationTile, d);        }        if(west){            this._pushToOpen(west, current, this.destinationTile, d);        }        if(this._open.length > 0){            var lowestF = this._open[0];            this._open.forEach(function(tile){                if((tile.hCost + tile.dCost) <= (lowestF.hCost + lowestF.dCost)){                    lowestF = tile;                }            }, this);            //keep searching for the end            this._iterativeSearch(lowestF, d + 1, shortestPath);        }else{            return [];        }                },    _pushToOpen: function(tile, current, dest, d){    	var physicsBody = this.map.getTile(tile.x/32, tile.y/32, 1);        if(Util.isTileEmpty(tile, this.world, this.entity) && \$.inArray(tile, this._closed) === -1 && \$.inArray(tile, this._open) === -1){            this._open.push(tile);            tile.parentTile = current;            tile.hCost = this._calcHCost(tile, current, dest);            tile.dCost = d;        }    },    _backtrack: function(shortestPath){        var currentTile = this.destinationTile;        while(!(currentTile === this.beginTile)){            shortestPath.unshift(currentTile);            var temp = currentTile;            currentTile = currentTile.parentTile;            delete temp.parentTile;            delete temp.hCost;            delete temp.dCost;        }        return shortestPath;    },    _calcHCost: function(tile, current, dest){        return Math.abs(this.pxToTiles(current.x) - this.pxToTiles(dest.x)) +         Math.abs(this.pxToTiles(current.y) - this.pxToTiles(dest.y));    },    pxToTiles: function(px){        return Math.floor(px/32);    }}`

Basically, it takes a world (containing a tile-map), a destination tile, and an entity, and finds the path from that entity to the destination tile along that tile-map.  Don't try to copy-paste this implementation, as it's got some implementation-specific stuff in it.  But it should provide a baseline for you to study A-star pathfinding.  Good luck!

~Cudabear