21Aug
Trails Animation
No commentsThis code was submitted to me via email who requested some assistance to get this trailing animation to be bound by any newly created object. I edited the code slightly and added a Rect class to build my rectangle object (which could be replaced by an image if so desired). I added a timer to reproduce the object, here is the resulting code/movie:
The Main.as:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends MovieClip
{
private var clip_arr:Array; // an array of clips
private var clip_ct:Number = 0;
private var clip_mc:Rect;
private const RADIUS:int=30;
private const FRICTION:Number=.95;
public function Main()
{
init();
}
private function init():void
{
stage.frameRate=31;
clip_arr = new Array();
var myTimer:Timer = new Timer(500, 50);
myTimer.addEventListener("timer", createClip);
myTimer.start();
}
private function createClip(e:TimerEvent):void
{
var randXOffset = Math.random() * stage.stageWidth;
var randYOffset = Math.random() * stage.stageHeight;
clip_arr.push ( new Rect() );
clip_arr[clip_ct].x=Math.cos(clip_arr[clip_ct].angle*1)*RADIUS + randXOffset;
clip_arr[clip_ct].y=Math.sin(clip_arr[clip_ct].angle*2)*RADIUS + randYOffset;
clip_arr[clip_ct].offsetX = randXOffset;
clip_arr[clip_ct].offsetY = randYOffset;
clip_arr[clip_ct].angle=0;
addChild(clip_arr[clip_ct]);
clip_arr[clip_ct].addEventListener(Event.ENTER_FRAME,go);
clip_ct++;
}
private function go(evt:Event):void
{
evt.target.x=Math.cos(evt.target.angle*2)*RADIUS + evt.target.offsetX;
evt.target.y=Math.sin(evt.target.angle*3)*RADIUS + evt.target.offsetY;
evt.target.angle+=.05;
var copy_mc:MovieClip=new Rect();
addChild(copy_mc);
copy_mc.x=evt.target.x;
copy_mc.y=evt.target.y;
copy_mc.addEventListener(Event.ENTER_FRAME,goCopy);
}
private function goCopy(evt:Event):void
{
evt.target.alpha*=FRICTION;
evt.target.scaleY=evt.target.alpha;
if(evt.target.alpha<=.3)
{
evt.target.removeEventListener(Event.ENTER_FRAME,goCopy);
var m:MovieClip=evt.target as MovieClip;
removeChild(m);
}
}
}
}
The rect class:
package
{
import flash.display.MovieClip;
public class Rect extends MovieClip
{
private var xPos:Number;
private var yPos:Number;
private var rWidth:Number;
private var rHeight:Number;
private var color:uint;
public var angle = Number;
public var offsetX = Number;
public var offsetY = Number;
public function Rect(xPos:Number=0,yPos:Number=0,rWidth:Number=10,rHeight:Number=10,color:uint=0x336699)
{
this.graphics.beginFill(color);
this.graphics.drawRect(xPos,yPos,rWidth,rHeight);
this.graphics.endFill();
this.angle = 0;
}
}
}
Categories: Actionscript 3, Animation Transitions, Arrays
Thursday, August 21st, 2008 at 8:53 am and is filed under Actionscript 3, Animation Transitions, Arrays. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.