A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: Making movieclip randomly move around a constrained area

  1. #1
    Senior Member
    Join Date
    Nov 2004
    Posts
    110

    Making movieclip randomly move around a constrained area

    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:
    Code:
    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:

    Code:
    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:

    Code:
    for (var p=0; p<array_circles.length; p++)
    {
    	if ( input_array[i] == 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!
    Last edited by insc; 03-11-2010 at 02:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center