Search code examples
actionscript-3flashvarmovieclip

ActionScript 3.0 Variable to Store MovieClip Name


I am learning Flash and ActionScript 3.0 for a once in a lifetime portfolio project. It has been quite a learning curve. At the moment I’m stuck trying to figure out how to create and call a variable that has the name of the movieclip I want to focus on.

I have two items on my stage. First is a movieclip (Animation01) that has the main contents I want to show. The second is a Play button (BtnPlay) and a Stop button (BtnStop) nested in a movieclip (MovPlayStop). The Play/Stop button allows the user to control whether to move through the project manually at their own pace or to play through the automated project animation.

When the user clicks on the Play button (BtnPlay), the user’s option is stored in a variable (AutoPlay) in the main timeline. It then plays a movieclip (Animation01) located on the main timeline:

//Play Button Function
import flash.events.MouseEvent;
stop();

BtnPlay.addEventListener(MouseEvent.CLICK, PlayBtn);
function PlayBtn(event:MouseEvent):void{
    MovieClip(root).AutoPlay = true;//Stores Autoplay value in the main timeline
    MovieClip(this.parent).Animation01.play();//Plays the movieclip on the main timeline
    gotoAndStop(2);//Switches the Play button to the Stop button
}

This works well for the current movieclip. However, I structured my project so that when the movieclip (Animation01) ends, it moves to the next frame in the main timeline which has the next movieclip in the series (Animation02). Movieclip (Animation02) finishes and we move to the next frame in the main timeline with the next movieclip.. and so on and on... How can I create a variable that has the path of the movieclip I want to focus on? Basically I want to create a variable for this line of code:

MovieClip(this.parent).Animation01.play();

I have tried creating the following at the beginning of the frame in the main timeline:

//As a String
var cFrame:String = "MovieClip(root).Animation01";


//As a MovieClip... not enough examples to look at
var cFrame:MovieClip = MovieClip(root).Animation01;

And then calling to it through the Play button function:

//New variable from main timeline variable
var Animation:MovieClip = MovieClip(root).cFrame;
Animation.play();


//Main timeline variable in the script
MovieClip(this.parent).MovieClip(root).cFrame.play();


//New variable from main timeline variable
var Animation:String = MovieClip(root).cFrame;
Animation.play();

No luck so far... Any ideas on how to create and call such a variable?


Solution

  • The script inside my button was doing this:

    MovieClip(this.parent).Animation01.play();
    

    Instead of referencing a static movieclip, I wanted to create a variable to replace the static movieclip. On the main timeline I created a variable called cFrame for the current Animation# movieclip. Everytime a new movieclip takes center stage, the value changes:

    //Main Timeline
    var cFrame:MovieClip = Animation01;
    
    //Next Frame
    cFrame = Animation02;
    

    Inside the Play button function, I simply called to the variable this way

    //Play Button
    MovieClip(this.parent).cFrame.play();
    

    This did it for me!

    The main issue that I was having was trying to include the "MovieClip(this.parent)" portion into the movieclip variable in the main timeline. I did not fully understand how parent child relationships work. Still a bit confused.