# limiting enemy movement

• Born
• Posts: 1

3+ Months Ago

I have a game where an enemy is following the tank (you). The POV is overhead.
Code: [ Select ]
spd = 0.3;
}

onClipEvent(enterFrame){ // rotate to face tank
Xdiff=_root.tank._x-_x;
Ydiff=_root.tank._y-_y;
updateAfterEvent();
//movement
if (_x != _root.tank._x || _y != _root.tank._y) {
x = Math.sin(_rotation*(Math.PI/180))*spd;
y = Math.cos(_rotation*(Math.PI/180))*spd*-1;
_x += x;
_y += y;
} else if (Math.sqrt((Xdiff) * (Xdiff) + (Ydiff) * (Ydiff)) >= 100) {
spd = 0;
}
}
2.     spd = 0.3;
3. }
4. onClipEvent(enterFrame){ // rotate to face tank
5.     Xdiff=_root.tank._x-_x;
6.     Ydiff=_root.tank._y-_y;
9.     updateAfterEvent();
10.     //movement
11.     if (_x != _root.tank._x || _y != _root.tank._y) {
12.         x = Math.sin(_rotation*(Math.PI/180))*spd;
13.         y = Math.cos(_rotation*(Math.PI/180))*spd*-1;
14.         _x += x;
15.         _y += y;
16.     } else if (Math.sqrt((Xdiff) * (Xdiff) + (Ydiff) * (Ydiff)) >= 100) {
17.         spd = 0;
18.     }
19. }

The problem is I want the enemy to stop at a certain distance from the tank, but I can't seem to do it. Any suggestions?
• Posts: 202

3+ Months Ago

switch the 2 IF statments

The first statement - which more likely than not will often be true - says unless the tank and this movie clip aren't at the same X or Y then calculate new location for this

The second statement - the Pythagorean theorem - should be your first test. However, it looks like you might have a typo - i think you mean to say less than or equal to

Switch them around and see if it gives you what you're looking for.

## Post Information

• Total Posts in this topic: 2 posts
• Users browsing this forum: No registered users and 5 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum