Groups | Blog | Home
all groups > flash actionscript > february 2005 >

flash actionscript : Detecting if any of a number of MC's are close


tralfaz
2/1/2005 6:07:05 PM
[quoted text, click to view]

That will be ok if you only have an X direction to check. If two
objects are at an angle, you have to use a calculation to get the
distance between them, like this:

a = clip1._x; // clip1's horizontal position
b = clip1._y; // clip1's vertical position
aa = clip2._x; // clip2's x position
bb = clip2._y; // clip2's y position
x = (a - aa); // how far apart are they in x direction?
y = (b - bb); // how far apart are they in y direction?
radius = math.sqrt((x * x) + (y * y)); // calculate the hypotoneuse
of the triangle
textbox.text = int(radius);

tralfaz

E1fjl
2/1/2005 10:22:51 PM
I have several movieclips on the stage and each one has similar code. They move
randomly and when two come within a certain proximity of each other i want them
to react to this. Is there a way to say: If (anyOtherMC is close) { //REACT
} Cheers, E1
christuf
2/1/2005 10:44:31 PM
Create many MC in your stage then use this code in the main timeline : for (z
in this) { if (typeof (this[z]) == 'movieclip') { for (y in this) { if
((typeof (this[y]) == 'movieclip') && this[z]._name != this[y]._name) {
if (this[z].hitTest(this[y])) { trace(this[z]._name +' hits
'+this[y]._name); } } } } } is that what you wanted ?
E1fjl
2/1/2005 10:58:32 PM
Thanks for the idea, i edited it a bit and ended up with: for(obj in _root){
if(typeof(_root[obj])=='movieclip'){ if(_root[obj]._name != this._name){
trace(_root[obj]._x - this._x) } } } Thanks a lot!
AddThis Social Bookmark Button