AS 2.0 help: string names to reference movie clips

Hello, normally I would give the topic a title describing the problem i’m having, but I can’t really describe it in a title.

I have a program where I am making various movieclips visible and invisible depending on stuff that is happening. Now I can do this like:

_root.movieclip1._visible = false;
_root.movieclip2._visible = true;

But I have a lot of this to do and its changing all the time. So what I want to be able to do is have a string variable, for example called ‘currentMovieclip’, that I can change the value of instead of having a line for making each movieclip visible/invisible. So it would look like this:

currentMovieclip = "movieclip1";
currentMovieclip._visible = false; 

That way I can change the value of the string to control which movieclips I am making visible/invisible rather than having to type out endless lines for each movieclip. Except, of course, that doesn’t work because you can’t make a string invisible.

So what I want to do is use a string (that changes to the names of different movieclips) in code, rather than the name of each individual movieclip name.

Hope this makes sense, and hope you can help.

Thanks, Script.ist

You can use array access notation:

a.b

Becomes

a[“b”]

Where you can also have a variable of a string in place of “b”

_root[currentMovieClip]._visible = false;

Thank you, Senocular, for your reply! :beam:

I got the code you posted to work, but I have another related problem:

[COLOR=#333333][FONT=Consolas]_root[currentMovieClip]._visible = false;[/FONT][/COLOR]

Works just fine. But I also want to be able to access variables from other classes. So I have created a variable in one class:

public static var block :Array = new Array;

Which I can access from another class just fine like:

If(MovieClip1.block[1] == (whatever)) then{

But I can’t access like:

If(_root["MovieClip1"].block[1] == (whatever)) then{

And I think this is because this doesn’t work:

If(_root.MovieClip1.block[1] == (whatever)) then{

So, why can you not access a public variable from another class when using _root and how do I solve this?

Thanks again, and hope you can help.

You’re exactly right. If _root.MovieClip1 doesn’t work, then neither will _root[“MovieClip1”]. It all depends where that variable MovieClip1 is defined. If you’re not prefixing it with another object to reference the variable from that object, that its more likely going to be this (otherwise probably global).

So what should probably work is:

If(this["MovieClip1"].block[1] == (whatever)) then{

Great! That works, thanks a lot :beam: