Skip navigation
Lenaped
Currently Being Moderated

Playing random movieClips onClick, then returning to start screen

Aug 13, 2012 5:15 PM

Hi- I'm very new to actionScript 3. My project starts out by plying one movie clip(startPage), then onClick, moviecip startPage disappears and a random movieclip(sc01-sc03) loads and plays. Once each random movieclip plays, I want to remove it from the stage and reload startPage. I have the random part figured out using an array, but I can't figure out how to remove the random sc movieClip once it is loaded. I tried using removeChild, but Flash isn't recognizing that I have added and of the sc movieclips as childen. Help!

 

My current code below:

 

package

{

          import flash.display.MovieClip;

          import flash.events.MouseEvent;

          import flash.events.Event;

 

          public class Main extends MovieClip

          {

                    var startPage:StartPage;

                    var sc01:Sc01;

                    var sc02:Sc02;

                    var sc03:Sc03;

 

 

 

                    private var _scenario:Array;

 

 

                    public function Main()

                    {

                              startPage = new StartPage();

                              sc01 = new Sc01();

                              sc02 = new Sc02();

                              sc03 = new Sc03();

 

 

                              _scenario = new Array();

 

 

 

                              MC.addChild(startPage);

 

                              //Add Scenes to array

                              _scenario[0] = sc01;

                              _scenario[1] = sc02;

                              _scenario[2] = sc03;

 

 

 

 

                              //Add Event Listeners

 

                              startPage.boxButton.addEventListener(MouseEvent.CLICK, onBoxButtonClick);

                              startPage.addFrameScript(startPage.totalFrames - 1, stopPageFunc);

                              sc01.addEventListener(Event.ENTER_FRAME, everyFrame01);

                              sc02.addEventListener(Event.ENTER_FRAME, everyFrame02);

                              sc03.addEventListener(Event.ENTER_FRAME, everyFrame03);

 

 

 

 

                              //stop at end of startPage timeline

 

                              function stopPageFunc()

                              {

                                            startPage.stop();

                              }

 

 

                              function onBoxButtonClick(event:MouseEvent):void

                              {

                                        MC.removeChild(startPage);

                                        MC.addChild(_scenario[Math.floor(Math.random()*_s cenario.length)]);

                              }

 

 

                              function everyFrame01():void

                              {

                                        if (sc01.currentFrame == 50)

                                        {

                                        MC.removeChild(sc01);

                                        MC.addChild(startPage);

                                        startPage.gotoAndPlay(1);

                                        }

                                        else

                                        {sc01.play();

                                        }

                              }

 

                              function everyFrame02():void

                              {

                                        if (sc02.currentFrame == 50)

                                        {

                                        MC.removeChild(sc02);

                                        MC.addChild(startPage);

                                        startPage.gotoAndPlay(1);

                                        }

                                        else

                                        {sc02.play();

                                        }

                              }

 

                              function everyFrame03():void

                              {

                                        if (sc03.currentFrame == 50)

                                        {

                                        MC.removeChild(sc03);

                                        MC.addChild(startPage);

                                        startPage.gotoAndPlay(1);

                                        }

                                        else

                                        {sc03.play();

                                        }

                              }

 

                    //go to startPage when scenario done

 

                    }

          }

 

}

 
Replies
  • kglad
    72,328 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 13, 2012 6:50 PM   in reply to Lenaped

    use:

     

     

    package

    {

              import flash.display.MovieClip;

              import flash.events.MouseEvent;

              import flash.events.Event;

     

              public class Main extends MovieClip

              {

                        var startPage:StartPage;

                       

                        private var _scenario:Array;

                        public function Main()

                        {

                                  startPage = new StartPage();

                                  _scenario = new Array();

                                  MC.addChild(startPage);

                                  //Add Scenes to array

                                  _scenario[0] = new Sc01();

                                  _scenario[1] = new Sc02();

                                  _scenario[2] = new Sc03();

    shuffle(_scenario);

                                  //Add Event Listeners

                                  startPage.boxButton.addEventListener(MouseEvent.CLICK, onBoxButtonClick);

                                  startPage.addFrameScript(startPage.totalFrames - 1, stopPageFunc);

                                  this.addEventListener(Event.ENTER_FRAME, everyFrame;

                           

                                  //stop at end of startPage timeline

                                  function stopPageFunc()

                                  {

                                                startPage.stop();

                                  }

     

     

                                  function onBoxButtonClick(event:MouseEvent):void

                                  {

    if(startPage.stage){

                                            MC.removeChild(startPage);

    }

    if(_scenario.length>0){

                                            MC.addChild(_scenario[0]);

    }

                                  }

     

     

                                  function everyFrame():void

                                  {

    if(_scenario[0].currentFrame==_scenario[0].totalFrames){

    MC.removeChild(_scenario[0]);

    _scenario.shift();

    if(_scenario.length==0){

    // startPage.gotoAndPlay(1); ???

    MC.addChild(startPage);

    }

    }

    }

    function shuffle(a:Array) {

        var i:int;

        var j:int;

        var e:*;

        var len:int = a.length;

        for (i = len-1; i>=0; i--) {

            j=Math.floor((i+1)*Math.random());

            e = a[i];

            a[i] = a[j];

            a[j] = e;

        }

    }

     

              }

     

    }

     
    |
    Mark as:
  • kglad
    72,328 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 13, 2012 9:43 PM   in reply to kglad

    use:

     

     

     

    package {

     

        import flash.display.MovieClip;

        import flash.events.MouseEvent;

        import flash.events.Event;

     

        public class Main extends MovieClip {

           

            var startPage:StartPage;

            private var _scenario:Array;

           

            public function Main() {

                startPage = new StartPage();

                MC.addChild(startPage);

                //Add Scenes to array

                scenarioF();

                //Add Event Listeners

                startPage.boxButton.addEventListener(MouseEvent.CLICK, onBoxButtonClick);

                startPage.addFrameScript(startPage.totalFrames - 1, stopPageFunc);

            }

            function scenarioF():void{

                _scenario = new Array();

                _scenario[0] = new Sc01();

                _scenario[1] = new Sc02();

                _scenario[2] = new Sc03();

                shuffle(_scenario);

            }

            function stopPageFunc() {

                startPage.stop();

            }

     

            function onBoxButtonClick(event:MouseEvent):void {

                if (startPage.stage) {

                    MC.removeChild(startPage);

                }

                if (_scenario.length>0) {

                    MC.addChild(_scenario[0]);

                    _scenario[0].addEventListener(Event.ENTER_FRAME,everyFrame);

                }

            }

     

            function everyFrame(e:Event):void {

                if (e.currentTarget.currentFrame==e.currentTarget.totalFrames) {

                    MC.removeChild(e.currentTarget);

                    e.currentTarget.removeEventListener(Event.ENTER_FRAME,everyFrame);

                    _scenario.shift();

                    if (_scenario.length==0) {

                        // startPage.gotoAndPlay(1); ???

                        // repopulate _scenario? if yes, call scenarioF();

                        MC.addChild(startPage);

                    }

                }

            }

     

            function shuffle(a:Array) {

                var i:int;

                var j:int;

                var e:*;

                var len:int=a.length;

                for (i = len-1; i>=0; i--) {

                    j=Math.floor((i+1)*Math.random());

                    e=a[i];

                    a[i]=a[j];

                    a[j]=e;

                }

            }

     

        }

    }

     
    |
    Mark as:
  • kglad
    72,328 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 14, 2012 5:10 PM   in reply to Lenaped

    there's no error on that line of code.

     

    attach a screenshot of the error and a screenshot of your actions panel at the frame mentioned in the error message.

     
    |
    Mark as:
  • kglad
    72,328 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 15, 2012 4:00 PM   in reply to Lenaped

    replace line 44 with:

     

    MC.removeChild(MovieClip(e.currentTarget));

     
    |
    Mark as:
  • kglad
    72,328 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 16, 2012 6:49 AM   in reply to kglad

    read the comments:

     

    // startPage.gotoAndPlay(1); ???

    // repopulate _scenario? if yes, call scenarioF();

     

    use:

     

     

     

    package {

     

        import flash.display.MovieClip;

        import flash.events.MouseEvent;

        import flash.events.Event;

     

        public class Main extends MovieClip {

           

            var startPage:StartPage;

            private var _scenario:Array=[];

           

            public function Main() {

                startPage = new StartPage();

                MC.addChild(startPage);

                //Add Scenes to array

                scenarioF();

                //Add Event Listeners

                startPage.boxButton.addEventListener(MouseEvent.CLICK, onBoxButtonClick);

                startPage.addFrameScript(startPage.totalFrames - 1, stopPageFunc);

            }

            function scenarioF():void{

                _scenario[0] = new Sc01();

                _scenario[1] = new Sc02();

                _scenario[2] = new Sc03();

                shuffle(_scenario);

            }

            function stopPageFunc() {

                startPage.stop();

            }

     

            function onBoxButtonClick(event:MouseEvent):void {

                if (startPage.stage) {

                    MC.removeChild(startPage);

                }

                if (_scenario.length>0) {

                    MC.addChild(_scenario[0]);

                    _scenario[0].addEventListener(Event.ENTER_FRAME,everyFrame);

                }

            }

     

            function everyFrame(e:Event):void {

                if (e.currentTarget.currentFrame==e.currentTarget.totalFrames) {

                    MC.removeChild(MovieClip(e.currentTarget));

                    e.currentTarget.removeEventListener(Event.ENTER_FRAME,everyFrame);

                    _scenario.shift();

                    if (_scenario.length==0) {

                        startPage.gotoAndPlay(1);

                       scenarioF();

                        MC.addChild(startPage);

                    }

                }

            }

     

            function shuffle(a:Array) {

                var i:int;

                var j:int;

                var e:*;

                var len:int=a.length;

                for (i = len-1; i>=0; i--) {

                    j=Math.floor((i+1)*Math.random());

                    e=a[i];

                    a[i]=a[j];

                    a[j]=e;

                }

            }

     

        }

    }

    p.s. please mark helpful/correct responses.

     
    |
    Mark as:
  • kglad
    72,328 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 16, 2012 7:23 PM   in reply to kglad

    what's the trace function reveal:

     

     

     

     

     

    package {

     

        import flash.display.MovieClip;

        import flash.events.MouseEvent;

        import flash.events.Event;

     

        public class Main extends MovieClip {

           

            var startPage:StartPage;

            private var _scenario:Array=[];

           

            public function Main() {

                startPage = new StartPage();

                MC.addChild(startPage);

                //Add Scenes to array

                scenarioF();

                //Add Event Listeners

                startPage.boxButton.addEventListener(MouseEvent.CLICK, onBoxButtonClick);

                startPage.addFrameScript(startPage.totalFrames - 1, stopPageFunc);

            }

            function scenarioF():void{

                _scenario[0] = new Sc01();

                _scenario[1] = new Sc02();

                _scenario[2] = new Sc03();

                shuffle(_scenario);

            }

            function stopPageFunc() {

                startPage.stop();

            }

     

            function onBoxButtonClick(event:MouseEvent):void {

    trace(_scenario.length,_scenario[0]);

                if (startPage.stage) {

                    MC.removeChild(startPage);

                }

                if (_scenario.length>0) {

                    MC.addChild(_scenario[0]);

                    _scenario[0].addEventListener(Event.ENTER_FRAME,everyFrame);

                }

            }

     

            function everyFrame(e:Event):void {

                if (e.currentTarget.currentFrame==e.currentTarget.totalFrames) {

                    MC.removeChild(MovieClip(e.currentTarget));

                    e.currentTarget.removeEventListener(Event.ENTER_FRAME,everyFrame);

                    _scenario.shift();

                    if (_scenario.length==0) {

                        startPage.gotoAndPlay(1);

                       scenarioF();

                        MC.addChild(startPage);

    trace("startPage added",startPage,startPage.stage)

                    }

                }

            }

     

            function shuffle(a:Array) {

                var i:int;

                var j:int;

                var e:*;

                var len:int=a.length;

                for (i = len-1; i>=0; i--) {

                    j=Math.floor((i+1)*Math.random());

                    e=a[i];

                    a[i]=a[j];

                    a[j]=e;

                }

            }

     

        }

    }

    p.s. please mark helpful/correct responses.

     
    |
    Mark as:
  • kglad
    72,328 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 17, 2012 12:05 PM   in reply to Lenaped

    use:

     

    package {

     

        import flash.display.MovieClip;

        import flash.events.MouseEvent;

        import flash.events.Event;

     

        public class Main extends MovieClip {

     

            var startPage:StartPage;

            private var _scenario:Array=[];

     

            public function Main() {

                startPage = new StartPage();

                MC.addChild(startPage);

                //Add Scenes to array

                scenarioF();

                //Add Event Listeners

                startPage.boxButton.addEventListener(MouseEvent.CLICK, onBoxButtonClick);

                startPage.addFrameScript(startPage.totalFrames - 1, stopPageFunc);

            }

            function scenarioF():void {

                _scenario[0] = new Sc01();

                _scenario[1] = new Sc02();

                _scenario[2] = new Sc03();

                shuffle(_scenario);

            }

            function stopPageFunc() {

                startPage.stop();

            }

     

            function onBoxButtonClick(event:MouseEvent):void {

                if (startPage.stage) {

                    MC.removeChild(startPage);

                }

                if (_scenario.length>0) {

                    MC.addChild(_scenario[0]);

                    _scenario[0].addEventListener(Event.ENTER_FRAME,everyFrame);

                }

            }

     

            function everyFrame(e:Event):void {

                if (e.currentTarget.currentFrame==e.currentTarget.totalFrames) {

                    MC.removeChild(MovieClip(e.currentTarget));

                    e.currentTarget.removeEventListener(Event.ENTER_FRAME,everyFrame);

                    _scenario.shift();

                    if (_scenario.length==0) {

                        startPage.gotoAndPlay(1);

                        scenarioF();

                        MC.addChild(startPage);

                    } else {

                        MC.addChild(_scenario[0]);

                        _scenario[0].addEventListener(Event.ENTER_FRAME,everyFrame);

                    }

                }

            }

     

            function shuffle(a:Array) {

                var i:int;

                var j:int;

                var e:*;

                var len:int=a.length;

                for (i = len-1; i>=0; i--) {

                    j=Math.floor((i+1)*Math.random());

                    e=a[i];

                    a[i]=a[j];

                    a[j]=e;

                }

            }

        }

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points