13Jun
Creating Multiple Objects with Actionscript
4 comments so farSo I have recently had a number of requests on how to create multiple objects of a class as well as how to manipulate them. I decided to write this short demo which will just create an object, which is in a file called c.as which happens to be just a circle. If you click the stage, the objects will simply just change their x and y positions of the stage.
Here is the document class, naming this file ObjectPlacement.as:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class ObjectPlacement extends Sprite
{
private var cArr:Array; // an array of all my circle objects
public function ObjectPlacement()
{
init();
}
private function init():void
{
// let's build 4 circles from our Circle class file -> c.as
// we will pass in a random radius and a random color
BuildCircles ( 10 );
// add a Stage Event Listener for Mouse Down
stage.addEventListener ( MouseEvent.MOUSE_DOWN, changePositions );
}
private function BuildCircles ( i:Number ):void
{
cArr = new Array();
for ( var ct:uint = 0; ct < i; ct++ )
{
// note: in the line below 'c' represents the class called 'c' - this class is in the file 'c.as'
cArr.push ( new c ( Math.random() * 20, Math.random() * 0xffffff ) );
// add this object to the stage
addChild ( cArr[ct] );
// move this object to some point on the stage
cArr[ct].x = Math.random() * stage.stageWidth;
cArr[ct].y = Math.random() * stage.stageHeight;
}
}
private function changePositions(m:MouseEvent):void
{
for ( var d:uint = 0; d < cArr.length; d++ )
{
cArr[d].x = Math.random() * stage.stageWidth;
cArr[d].y = Math.random() * stage.stageHeight;
}
}
}
}
Here is the object class called ‘c’ and saved as ‘c.as’:
package {
/* this class will simply draw a circle */
import flash.display.Sprite;
public class c extends Sprite {
public var radius:Number;
private var color:uint;
public function c(radius:Number=20, color:uint=0xb3d830)
{
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
Categories: Actionscript 3, Drawing API
Friday, June 13th, 2008 at 10:31 am and is filed under Actionscript 3, Drawing API. 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.
awesome work! I wanted to know how I can have multiple movie clips have it’s own motion or tween to each one. I have this http://www.chandlerr.com/ar/fp1/tester.htm which is just one movie clip generated by the .as file. The .as file is mainly so i can have that trail effect. (I didn’t write this script)… But, It has a random motion to it and i can manipulate that motion but only slow it down, reduce the circular motion, etc. I need the logo to travel on a specific path, or curved path. i did this to show as an example of what I need the logos to act. http://www.chandlerr.com/ar/fp1/main.htm
My question is, can I have multiple logos fly in one after the other from different directions with that trail effect?
Any knowledge would be helpful!
Thanks,
Chandler
chandlerr.com
Thanks for your comments Chandler -
There is definitely a solution to get multiple logos to fly in one after another as well as to apply the trail effect. Have you ever worked with Tweener for you animation? If not, you should check it out. Since you have the trail effect you should be able to add this animated effect to your new code. This gets me thinking that I would like to take these ideas and set up some demos in the near future. Would you like to post your code progress at this point?
Thanks again, Morgan
HI! Thanks for the great news!! If you can’t tell I am new to flash period. So no I have not used Tweener for my animations. I will have to learn everything about it because I have not looked into it. I really want to learn this and need to quickly. Do you think you could do one logo for me so I can look at it when I learn so it will help me have a better understanding of it?
here is the code in the .as file…
——————————————–
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip
{
private var clip_mc:MovieClip;
private const RADIUS:int=10;
private const FRICTION:Number=.95;
public function Main()
{
init();
}
private function init():void
{
stage.frameRate=31;
createClip();
}
private function createClip():void
{
clip_mc=new Clip();
clip_mc.x=stage.stageWidth/2+Math.cos(clip_mc.angle*1)*RADIUS;
clip_mc.y=stage.stageHeight/2+Math.sin(clip_mc.angle*2)*RADIUS;
clip_mc.angle=0;
addChild(clip_mc);
clip_mc.addEventListener(Event.ENTER_FRAME,go);
}
private function go(evt:Event):void
{
evt.target.x=stage.stageWidth/2+Math.cos(clip_mc.angle*2)*RADIUS;
evt.target.y=stage.stageHeight/2+Math.sin(clip_mc.angle*3)*RADIUS;
evt.target.angle+=.05;
var copy_mc:MovieClip=new Clip();
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);
}
}
}
}
——————————–
This was something i was looking for. This will help me a lot. Thanks