Skip to main content
Known Participant
June 22, 2011
Answered

follow a object and do somthing when in a distance

  • June 22, 2011
  • 1 reply
  • 394 views

how can i make a movieclip folow a nother movieclip and when it is in a distance stop and then do somthing?

can you give me a sample code

Thanks

This topic has been closed for replies.
Correct answer Colin Holgate

Can you say where in the world you are? There are a number of email lists and user groups, that you should definitely join. People love these sorts of questions, but here on a mobile specific area the questions might be overlooked.

But I love these sorts of questions too! So, do you remember a guy named Pythagoras? He wasn't familiar with ActionScript, but I think he would have been good at coding. He's the one that worked out how to tell the diagonal of a right angled triangle. It comes in really useful when animating objects.

Here's how you might have one thing follow another, in say an enterframe script:

mc2.x = mc2.x+(mc1.x-mc2.x)/5;

mc2.y = mc2.y+(mc1.y-mc2.y)/5;

Here's how to see that they are less than 20 pixels apart:

var dx:Number = mc1.x-mc2.x;

var dy:Number = mc1.y-mc2.y;

var d:Number = Math.sqrt(dx*dx+dy*dy);

if(d<20) {

  //stop moving mc2

}

It might take up too much time to set up an example for you, but do you get the idea?

1 reply

Colin Holgate
Colin HolgateCorrect answer
Inspiring
June 22, 2011

Can you say where in the world you are? There are a number of email lists and user groups, that you should definitely join. People love these sorts of questions, but here on a mobile specific area the questions might be overlooked.

But I love these sorts of questions too! So, do you remember a guy named Pythagoras? He wasn't familiar with ActionScript, but I think he would have been good at coding. He's the one that worked out how to tell the diagonal of a right angled triangle. It comes in really useful when animating objects.

Here's how you might have one thing follow another, in say an enterframe script:

mc2.x = mc2.x+(mc1.x-mc2.x)/5;

mc2.y = mc2.y+(mc1.y-mc2.y)/5;

Here's how to see that they are less than 20 pixels apart:

var dx:Number = mc1.x-mc2.x;

var dy:Number = mc1.y-mc2.y;

var d:Number = Math.sqrt(dx*dx+dy*dy);

if(d<20) {

  //stop moving mc2

}

It might take up too much time to set up an example for you, but do you get the idea?

Known Participant
June 22, 2011

Yes Thanks