Copy link to clipboard
Copied
Well this is the thing, I have an array of Moviecclips, and I want a random movieclip from that array to appear on stage every certain amount of time.
I made a class that adds a child to the variable, the problem is that sometimes the instances disappear.
here's the code of the class:
import MC1 //the movieclips
import MC2
import MC3
public class theclass extends MovieClip
{
var arrayA:Array = new Array(MC1,MC2,MC3);
var containerM:MovieClip;
var Speed:int = 7
public function theclass()
{
containerM = arrayA[int(Math.random()*3)];
this.addChild(containerM);
addEventListener(Event.ENTERR_FRAME, eFrame);
}
private function eFrame(event:Event):void
{
this.x -= Speed;
if(this.x < 0 - this.width)
{
removeEventListener(Event.ENTER_FRAME,eFrame);
if(this.parent)
{
this.parent.removeChild(this);
}
}
Now I call it from stage:
var min:int = 0;
var limit:int = 40;
function repeat(event:Event):void
{
min++
if(min>= limit)
{
var MoC:theclass = new theclass();
Moc.x = stage.stageWidth;
Moc.y = stage.stageHeight/2;
addChild(Moc);
min = 0;
}
}
stage.addEventListener(Event.ENTER_FRAME,repeat);
So here's basically the code, so can anyone tell me why the instance disappears before getting to the other side of the stage? or what's the problem?
yes, this would work:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class theClass extends MovieClip {
var arrayA: Array = new Array(MC1, MC2, MC3, MC4, MC5, MC6); // <- where these are class names
var containerM: MovieClip;
var Speed: int = 7
public function theClass() {
containerM = new arrayA[int(Math.random() * 3)];
this.addChild(containerM);
addEventListener(Event.ENTER_FRAME, eFrame);
...
Copy link to clipboard
Copied
there are, at least, 3 errors that would prevent that code from compiling/running.
assuming your code is error-free, copy and paste your code.
Copy link to clipboard
Copied
Yes I've noticed the MoC, but apart from that the code is exactly the same. I think those are the errors you mean.
Copy link to clipboard
Copied
2. there's no Event.ENTERR_FRAME event and
3. it makes no sense trying to add class names to the display list (though if those are just poorly chosen instance names that would work but would cause your problem because you only have 3 instances so when you add one that's already been used, it will be removed from the display).
Copy link to clipboard
Copied
Oh, sorry didn't see the event, but what you say on point three makes sense, well actually I have 6 movieclips in the array but didn't see the point on writting them all until now, sometimes the instance disappears when the other comes out so it really makes sense, is there a way to make this right?
Copy link to clipboard
Copied
yes, this would work:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class theClass extends MovieClip {
var arrayA: Array = new Array(MC1, MC2, MC3, MC4, MC5, MC6); // <- where these are class names
var containerM: MovieClip;
var Speed: int = 7
public function theClass() {
containerM = new arrayA[int(Math.random() * 3)];
this.addChild(containerM);
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function eFrame(event: Event): void {
this.x -= Speed;
if(this.x < 0 - this.width) {
removeEventListener(Event.ENTER_FRAME, eFrame);
if(this.parent) {
this.parent.removeChild(this);
containerM=null
}
}
}
}
}
// p.s. please mark helpful/correct responses.
Copy link to clipboard
Copied
Here is how the code is right now and they keep disappearing:
import flash.display.MovieClip;
import flash.events.Event;
import MC1 //the movieclips
import MC2
import MC3
import MC4
import MC5
import MC6
public class theclass extends MovieClip
{
static var mc1:MC1 = new MC1();
static var mc2:MC2 = new MC2();
static var mc3:MC3 = new MC3();
static var mc4:MC4 = new MC4();
static var mc5:MC5 = new MC5();
static var mc6:MC6 = new MC6();
var arrayA:Array = new Array(mc1,mc2,mc3,mc4,mc5,mc6);
var containerM:MovieClip;
var Speed:int = 7
public function theclass()
{
containerM = arrayA[int(Math.random()*6)];
this.addChild(containerM);
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function eFrame(event:Event):void {
this.x -= Speed;
if(this.x < 0 - this.width)
{
removeEventListener(Event.ENTER_FRAME,eFrame);
if(this.parent)
{
this.parent.removeChild(this);
containerM = null;
}
}
Copy link to clipboard
Copied
copy the code i suggested.
Copy link to clipboard
Copied
You were right, thanks, didn't check in detail your code after all.
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now