JCR10
JCR10

Reputation: 79

how to access variables from class.as to use in a movieclip

i have a class name sample_1.as

package  {

    import flash.display.MovieClip;


    public class sample_1 extends MovieClip {

        public var targetScene:String;

        public function sample_1() {
            // constructor code
        }
    }
}

and i want to access and change the targetScene string from different frames and also inside movieclips like

gotoAndPlay(targetScene);
or
targetScene = "MainMenuEnter";

how can i do that?

Upvotes: 0

Views: 278

Answers (1)

Simon McArdle
Simon McArdle

Reputation: 893

A quick and easy way to have a variable like that available from any frame/class is to make it static:

public class sample_1 extends MovieClip {    
    public static var targetScene:String;
}

You can access targetScene from anywhere using sample_1.targetScene, for example:

sample_1.targetScene = "3";
gotoAndPlay(uint(sample_1.targetScene));

It's not the best method to use, especially as a project gets bigger and has more components, declaring variables static for the sake of global access can lead to a lot of spaghetti code but it's a quick 'n' dirty fix to get you started.

Upvotes: 1

Related Questions