# Making movieclip randomly move around a constrained area

**URL:** <https://forum.kirupa.com/t/making-movieclip-randomly-move-around-a-constrained-area/306210>\
**Category:** flash\
**Created:** [March 11, 2010, 6:35am UTC](https://forum.kirupa.com/t/making-movieclip-randomly-move-around-a-constrained-area/306210 "2010-03-11T06:35:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcabaco](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@tcabaco](https://forum.kirupa.com/u/tcabaco)\
**Post date:** [March 11, 2010, 6:35am UTC](https://forum.kirupa.com/t/making-movieclip-randomly-move-around-a-constrained-area/306210/1 "2010-03-11T06:35:49Z")

</div>

Hi everyone,

I have an array of display objects that are simple scattered circles on the stage. My goal is to have them move around randomly within, say, a 5/10 pixel radius/range from their original x and y. Sort of like fireflies, but not as jittery and fast, I’m looking for something real smooth.

What I have now is something like this:

```auto

for (var g=0; g < total_circles; g++)
{
circle = new DrawCircle( 3, 0xFFFFFF );
circle.x = random; circle.y = random;
addChild(circle);
jitter = JitterCircle( circle, 10 );
}

```

So as you can see for each circle I call a class JitterCircle and pass it two values: the actual display object and the range I want my circle to fly around in.  
Inside JitterCircle I have this:

```auto

package 
{
    import flash.display.*;
    import flash.events.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    
    public class JitterCircle 
    {
        private var circle:DisplayObject;
        private var cX:Number;
        private var cY:Number;
        private var range:Number;
        
        private var tx:Number;
        private var ty:Number;
        private var xtemp:Number;
        private var ytemp:Number;
        private var xd:Number;
        private var yd:Number;
        private var x0:Number;
        private var y0:Number;
        
        public function JitterCircle( target:DisplayObject, _range:Number ) 
        {
            circle = target;
            
            tx = Math.random()*360; 
            ty = Math.random()*360; 
 
            xtemp = Math.random()/7; 
            ytemp = Math.random()/7; 

            xd = Math.random()*10+1; 
            yd = Math.random()*10+1; 
            x0 = circle.x; 
            y0 = circle.y; 
            
            circle.addEventListener( Event.ENTER_FRAME, on_frame );
        }
                
        private function on_frame( e:Event ):void {            
            var mc = e.currentTarget;
               tx += xtemp;
               ty += ytemp;
               mc.x = x0 + Math.sin( tx ) * xd;
               mc.y = y0 + Math.cos( ty ) * yd;
        }
    }
}

```

This actually works, this does make it fly around neatly like a drugged down firefly. However here’s my issue:  
Now I have a series of actions on the site, and one of which is call out some of these circles and make those selected go to a certain position, but in order to do so I need to stop/remove that ENTER\_FRAME listener inside the JitterCircle. How do I do this?  
What I have so far from this action is a selection of the circles being moved, which goes something like this:

```auto

for (var p=0; p<array_circles.length; p++)
{
    if ( input_array* == array_circles[p].lpu.letter.text )
    {
        used_circles_array.push( array_circles[p] );
    }
}

```

I figure there’s something I need to do here to have it stop/remove the listener from these selected circles. I just can’t get my head around the how!!  
I do I go IN that class, identify THAT particular circle and remove its ENTER\_FRAME listener?

Thanks in advance for the help! Really appreciate it!
