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

Playing random movieClips onClick, then returning to start screen

Community Beginner ,
Aug 13, 2012 Aug 13, 2012

Copy link to clipboard

Copied

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()*_scenario.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

 

                    }

          }

 

}

TOPICS
ActionScript

Views

2.6K

Translate

Translate

Report

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 , Aug 15, 2012 Aug 15, 2012

replace line 44 with:

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

Votes

Translate

Translate
Community Expert ,
Aug 13, 2012 Aug 13, 2012

Copy link to clipboard

Copied

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;

        a = a;

        a = e;

    }

}

          }

}

Votes

Translate

Translate

Report

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 Beginner ,
Aug 13, 2012 Aug 13, 2012

Copy link to clipboard

Copied

Thank you!

I keep getting this error though:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

          at flash.display::DisplayObjectContainer/removeChild()

          at Function/<anonymous>()

I get this error when Flash tries to remove the random sc movieclip from the stage. I got this a bunch using my other code too. Any idea what is causing this? Is this because I have called up my sc movieclips with an array and Flash doesn't know where to find it?

Also, when my sc movieclips play on the stage, they don't play from the beginning. It's as if they were already playing before they were added to the stage. Do you know how to fix this?

Thanks so much for your help!

Votes

Translate

Translate

Report

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 ,
Aug 13, 2012 Aug 13, 2012

Copy link to clipboard

Copied

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;

                a=a;

                a=e;

            }

        }

    }

}

Votes

Translate

Translate

Report

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 Beginner ,
Aug 14, 2012 Aug 14, 2012

Copy link to clipboard

Copied

I get an error 1118: Implicit coercion of a value with static typr Object to a possible unrelated type flash.DisplayObject on line 57:  var j:int;

Any ideas?

Thanks!

Votes

Translate

Translate

Report

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 ,
Aug 14, 2012 Aug 14, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 Beginner ,
Aug 15, 2012 Aug 15, 2012

Copy link to clipboard

Copied

I get the error at line 44 and 57. Thanks for all your help!errorScreenshot.jpg

Votes

Translate

Translate

Report

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 ,
Aug 15, 2012 Aug 15, 2012

Copy link to clipboard

Copied

replace line 44 with:

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

Votes

Translate

Translate

Report

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 Beginner ,
Aug 15, 2012 Aug 15, 2012

Copy link to clipboard

Copied

Thanks- I'm not getting any errors any more and playing random scenes is working once the boxButton is clicked, but it's still not behaving how I would like. Once, each random scene is done playing, I want startPage to play again from the beginning, and start the whole process over again. Currently, once the randomly added sc finishes playing, the screen goes blank. ???

Votes

Translate

Translate

Report

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 ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

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;

                a=a;

                a=e;

            }

        }

    }

}

p.s. please mark helpful/correct responses.

Votes

Translate

Translate

Report

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 Beginner ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

That doesn't make a difference. I'm still not getting any errors and still not returning to startPage once random sc plays.

Votes

Translate

Translate

Report

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 ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

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;

                a=a;

                a=e;

            }

        }

    }

}

p.s. please mark helpful/correct responses.

Votes

Translate

Translate

Report

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 Beginner ,
Aug 17, 2012 Aug 17, 2012

Copy link to clipboard

Copied

Nada. The output window shows up blank.

Votes

Translate

Translate

Report

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 ,
Aug 17, 2012 Aug 17, 2012

Copy link to clipboard

Copied

LATEST

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;

                a=a;

                a=e;

            }

        }

    }

}

Votes

Translate

Translate

Report

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