# AS 2.0 help: string names to reference movie clips

**URL:** <https://forum.kirupa.com/t/as-2-0-help-string-names-to-reference-movie-clips/351939>\
**Category:** flash\
**Created:** [October 19, 2014, 6:34pm UTC](https://forum.kirupa.com/t/as-2-0-help-string-names-to-reference-movie-clips/351939 "2014-10-19T18:34:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Script\_ist](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@Script\_ist](https://forum.kirupa.com/u/Script_ist)\
**Post date:** [October 19, 2014, 6:34pm UTC](https://forum.kirupa.com/t/as-2-0-help-string-names-to-reference-movie-clips/351939/1 "2014-10-19T18:34:23Z")

</div>

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:

```auto
_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:

```auto
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

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [October 20, 2014, 12:33pm UTC](https://forum.kirupa.com/t/as-2-0-help-string-names-to-reference-movie-clips/351939/2 "2014-10-20T12:33:19Z")

</div>

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”

```auto
_root[currentMovieClip]._visible = false;

```

---

<div class="post-metadata">

**Author:** ![Script\_ist](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@Script\_ist](https://forum.kirupa.com/u/Script_ist)\
**Post date:** [October 22, 2014, 4:25pm UTC](https://forum.kirupa.com/t/as-2-0-help-string-names-to-reference-movie-clips/351939/3 "2014-10-22T16:25:03Z")

</div>

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

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

```auto
[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:

```auto
public static var block :Array = new Array;

```

Which I can access from another class just fine like:

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

```

But I can’t access like:

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

```

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [October 22, 2014, 4:58pm UTC](https://forum.kirupa.com/t/as-2-0-help-string-names-to-reference-movie-clips/351939/4 "2014-10-22T16:58:40Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Script\_ist](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@Script\_ist](https://forum.kirupa.com/u/Script_ist)\
**Post date:** [October 22, 2014, 7:52pm UTC](https://forum.kirupa.com/t/as-2-0-help-string-names-to-reference-movie-clips/351939/5 "2014-10-22T19:52:21Z")

</div>

Great! That works, thanks a lot :beam:
