Skip to main content
Alex_95
Participating Frequently
June 22, 2014
Answered

Instances disappearing

  • June 22, 2014
  • 1 reply
  • 371 views

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?

This topic has been closed for replies.
Correct answer kglad

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?


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.

1 reply

kglad
Community Expert
Community Expert
June 22, 2014

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.

Alex_95
Alex_95Author
Participating Frequently
June 22, 2014

Yes I've noticed the MoC, but apart from that the code is exactly the same. I think those are the errors you mean.

kglad
Community Expert
Community Expert
June 22, 2014

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).