function Direction(){ GetDist(); if (this.distx<= this.disty){ if (this.testy<y) dir=0; if (this.testy>y) dir=2; }else{ if (this.testx<x) dir=1; if (this.testx>x) dir=3; } } |
The first thing this function does is call GetDist() . This of course is so it knows its target's x y cords ( this.testx and this.testy) as well as the distance to the target. Then what it does from there is compare the x and y distances. If the x distance (this.distx ) is less than the y distance (this.disty), then it will check to see if the target is above or below the baddy; depending on which is true, the dir will be set appropriately. But if in the first place this.distx is greater than this.disty it goes to the else statement to determine if the target is to the left or right of the baddy, and sets its dir accordingly. |