# JavaScript movement of player

Okay so I am quite new, I've got the player to move using up, down, right, and left. But what I am trying to do is make the character move with a left click.

var Player = function(id){
var self = {
x:250,
y:250,
id:id,
number:"" + Math.floor(10 * Math.random()),
pressingRight:false,
pressingLeft:false,
pressingUp:false,
pressingDown:false,
maxSpd:10,
}
self.updatePosition = function(){
if(self.pressingRight)
self.x += self.maxSpd;
if(self.pressingLeft)
self.x -= self.maxSpd;
if(self.pressingUp)
self.y -= self.maxSpd;
if(self.pressingDown)
self.y += self.maxSpd;
}
return self;
}


That's what I have for the up, down, left, right movement. I've seen a few documents stating the click but just can't seem to implement it into the file.

• Move to the location you clicked? Or did you mean something else? – Engineer Mar 19 '17 at 15:27
• That's right, Instead of the left,right,up,down that i have i want to make it so that the player clicks to move to the location clicked. – CheekyTrooper Mar 19 '17 at 15:39

## 1 Answer

To do that, you'll have to look into AI pathfinding. "Hold up," you say, "I don't even have any enemies yet! How can I need AI?".

Once you start talking about "go where I click" then you have to deal with a great deal more than is immediately obvious: obstacle avoidance, finding out whether there even is a path (imagine you're between 4 walls), best path etc. for your player to actually be able to get where you click. After all, you're not just moving as the crow flies... are you?

I suggest starting with basic A* pathfinding (note: there are many, many sources of info on this around the web). You can also try hill-climbing which in some ways is a little simpler to get working - depending on how simple your game is / how many enemies are involved. The good news is you will then have the ability to get your enemies to also go wherever you want them.

• Thankyou very much, I shall look into this – CheekyTrooper Mar 19 '17 at 15:56