|
-
Loading several MovieClips in to seperate variables?
Hello
I have several Movie Clips in the Library which I need to be able to load and bring up on the screen randomly.
The code below shows how I can load one of these, but to get a proper control ideally I would like to load them all in to an array or numbered variables so that they can be called from a simple piece of code and actioned one at a time.
My movieclips can all be called something simple like mc1, mc2, mc3, mc4 etc to make things easier.
These need to be referenced in to an array mcContainer[.....] or as mcContainer1, mcContainer2........ so that they can be called easily.
I just need to know the simplest way to do this and how i reference them, I have tried several ways like ["mc"+i] etc and nothing seems to work.
Code:
var mc:mcLogo=new mcLogo();
addChild(mc);
Thanks in advance for any help.
Paul
-
Senior Member
Here's one solution, I'll try to comment as much as I can,
PHP Code:
var mc1:m1 = new m1(); // each mc is associated with its' own class, whose base class is MovieClip(); var mc2:m2 = new m2(); var mc3:m3 = new m3(); var mc4:m4 = new m4();
var mcArray:Array = new Array(mc1, mc2, mc3, mc4); // each mc is placed inside an array var rNum:int = Math.random() * mcArray.length; // a random number between 0 and 3 is produced, based on the amount of indicies in the array
addChild(mcArray[rNum]); // the randomly selected mc is then added to the stage
HTH.
Wile E. Coyote - "Clear as mud?"
-
Below is all the code that I have played around with and get the error
"TypeError: Error #1007: Instantiation attempted on a non-constructor."
Code:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.setTimeout;
var mc0:mcClip0 = new mcClip0(); // each mc is associated with its' own class, whose base class is MovieClip();
var mc1:mcClip1 = new mcClip1();
var mcList:Array = new Array [mc0, mc1];
var imgCount:uint = 2;
var currentImage:uint = 0;
var myTimer:Timer = new Timer(1500, 1);
//var mc:MovieClip;
var xPos:Number = stage.stageWidth/2;
var yPos:Number = stage.stageHeight/2;
//var mc:MovieClip=new mcList[currentImage]();
//addChild(mc);
//mc.x= Math.floor(Math.random() * (stage.stageWidth-(mc.width*1.5))) + (mc.width/2);//xPos;
//mc.y= Math.floor(Math.random() * (stage.stageHeight-(mc.height*1.5))) + (mc.width/2);//yPos;
mcLogoStart();
addChild(mcList[currentImage]);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerHandler);
function mcLogoStart():void{
mcList[currentImage].x= Math.floor(Math.random() * (stage.stageWidth-(mcList[currentImage].width*1.5))) + (mcList[currentImage].width/2);//xPos;
mcList[currentImage].y= Math.floor(Math.random() * (stage.stageHeight-(mcList[currentImage].height*1.5))) + (mcList[currentImage].width/2);//yPos;
var mcLogoTween:Tween = new Tween(mcList[currentImage], "alpha", None.easeIn, 0, 1, 2, true);
mcLogoTween.addEventListener(TweenEvent.MOTION_FINISH, freezeImage);
}
function freezeImage(e:TweenEvent):void{
//
if(mcList[currentImage].alpha==1){
myTimer.start();
}
if(mcList[currentImage].alpha==0){
currentImage++;
if(currentImage==imgCount){
currentImage=0;
}
addChild(mcList[currentImage]);
//mc:MovieClip=new mcList[currentImage];
//addChild(mc);
mcLogoStart();
}
}
function timerHandler(e:TimerEvent):void{
mcLogoFinish();
}
function mcLogoFinish():void{
var mcLogoTweenOut:Tween = new Tween(mcList[currentImage], "alpha", None.easeOut, 1, 0, 2, true);
mcLogoTweenOut.addEventListener(TweenEvent.MOTION_FINISH, freezeImage);
}
-
Senior Member
This line
PHP Code:
var mcList:Array = new Array [mc0, mc1];
should read as such
PHP Code:
var mcList:Array = new Array(mc0, mc1);
Wile E. Coyote - "Clear as mud?"
-
Excellent works a treat.
Many thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|