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 = {
        number:"" + Math.floor(10 * Math.random()),
    self.updatePosition = function(){
            self.x += self.maxSpd;
            self.x -= self.maxSpd;
            self.y -= self.maxSpd;
            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.

  • \$\begingroup\$ Move to the location you clicked? Or did you mean something else? \$\endgroup\$
    – Engineer
    Commented Mar 19, 2017 at 15:27
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 19, 2017 at 15:39

1 Answer 1


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.

  • \$\begingroup\$ Thankyou very much, I shall look into this \$\endgroup\$ Commented Mar 19, 2017 at 15:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .