View Single Post
  #2  
Old 05-28-2012, 02:31 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Use the distance formula:



Therefore, to get distance

PHP Code:
function dist(x1y1x2y2) {
  return (((
x1 x2) ^ 2) + ((y1 y2) ^ 2)) ^ 0.5;

So you can just do...

PHP Code:
if (this.dist(player.xplayer.ythis.xthis.y) < 5) {
  
player.chat "I'm within 5 tiles!";
} else {
  
player.chat "I'm not within 5 tiles!";

If you'd prefer to check based on tiles (e.g. check in a square around the NPC as opposed to a circle with distance), you can do:

PHP Code:
if (player.x in |this.2.5this.2.5| && player.y in |this.2.5this.2.5|) {
  
player.chat "I'm in the 5-tile box around the NPC.";
} else {
  
player.chat "I'm not in the 5-tile box around the NPC.";

I would typically choose the first, but it obviously depends on what you're trying to accomplish.

You'll probably want to adjust the player's x/y and the object's x/y to reach the center of the object.
__________________
Reply With Quote