1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Move to the location you clicked? Or did you mean something else? \$\endgroup\$ – Engineer Mar 19 '17 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\$ – CheekyTrooper Mar 19 '17 at 15:39
2
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.