Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Instances disappearing

New Here ,
Jun 21, 2014 Jun 21, 2014

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?

TOPICS
ActionScript
327
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 22, 2014 Jun 22, 2014

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

     

...
Translate
Community Expert ,
Jun 21, 2014 Jun 21, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 21, 2014 Jun 21, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2014 Jun 21, 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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 21, 2014 Jun 21, 2014

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 22, 2014 Jun 22, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 22, 2014 Jun 22, 2014

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;

               } 

     } 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 22, 2014 Jun 22, 2014

copy the code i suggested.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 22, 2014 Jun 22, 2014

You were right, thanks, didn't check in detail your code after all.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 22, 2014 Jun 22, 2014
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines