Skip to main content
Participant
May 11, 2009
Question

Agro range on following code

  • May 11, 2009
  • 1 reply
  • 446 views

A couple weeks ago, I was assissted by kglad in recieving a code that makes enemy MC's follow a player mc controlled with the arrow keys, and food MC that moves away from the player. the code for the food is as follows:

var runBallA:Array = [food1MC,food2MC,food3MC];

for(var i:Number=0;i < runBallA.length;i++){
     runBallA.speed = -3;
}
runBallI = setInterval(runBallF,40);

function runBallF(){
     for(var i:Number=0;i < runBallA.length;i++){
          var ang:Number = Math.atan2(runBallA._y-player._y,runBallA._x-player._x);
          runBallA._x += Math.cos(Math.PI+ang)*runBallA.speed;
          runBallA._y += Math.sin(Math.PI+ang)*runBallA.speed;
     }
     updateAfterEvent();
}

my question is how do i put an agro range on this code so that the foodMC moves away from the player only when the player is within (i.e.) 100 pixels?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
May 11, 2009

i don't know what agro means but you can use an if-statement to check player-object proximity.

Participant
May 11, 2009

Yeah, I mean proximity. So should i put my if statement before function runballF?

and what would be the best way to set that up

kglad
Community Expert
Community Expert
May 11, 2009

check proximity of each object in the for-loops first line to see if you should calculate ang and update an object's _x,_y properites.