Skip to main content
Participating Frequently
June 17, 2013
Answered

Can you use a variable in place of movieclip linkage for addchild?

  • June 17, 2013
  • 1 reply
  • 529 views

var top:MovieClip = MovieClip(root);

top.menu3.addChild(member[i-1].scenarioplayed);

I want to add components to a movieclip depending on value of i.

The movieclips are named menu3, menu4, etc.

Is there a way to make all are part of "top.menu3" a variable?

I tried this but got a compile error:

Symbol 'scene3', Layer 'Actions', Frame 1, Line 1611061: Call to a possibly undefined method addChild through a reference with static type String.

var menuName:String;

menuName = "top.menu5";

menuName.addChild(member[i-1].scenarioplayed);

This topic has been closed for replies.
Correct answer Ned Murphy

For the code you just showed, menuName is a String.  String do not have an addChild method.

It is not clear what you are working with to get what you want, but if you want to use String values to target objects you can use the bracket notation...

var menuObject:MovieClip = top["menu5"];

menuObject.addChild(member[i-1].scenarioplayed);



1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
June 17, 2013

For the code you just showed, menuName is a String.  String do not have an addChild method.

It is not clear what you are working with to get what you want, but if you want to use String values to target objects you can use the bracket notation...

var menuObject:MovieClip = top["menu5"];

menuObject.addChild(member[i-1].scenarioplayed);


Participating Frequently
June 17, 2013

Can menu5 then be a variable?

var menu:string = "menu5";

var menuObject:MovieClip = top[menu];        // Can variable be placed here

menuObject.addChild(member[i-1].scenarioplayed);

I went to use a switch statement to send component to right movieclip.

switch (true)

            {

                      case i > 34:

                        {

                                        q = 34;

                                        menu = "menu5";

                                        break;

                              }

                      case i > 17:

                        {

                                        q = 17;

                                        menu = "menu4";

                                        break;

                              }

                     case i < 18:

                       {

                                 q = 0;

                                 menu = "menu3";

                                 break;

                       }

then add following for placement

var menuObject:MovieClip = top[menu];        // Can variable be placed here

menuObject.addChild(member[i-1].scenarioplayed);

Participating Frequently
June 17, 2013

BTW, I have the project I am working on up at http://www.ragtopvideo.com/testpage.html

The team link is the only one working now.  It can do up to 17 team members, this part

is meant to spill over larger numbers onto other pages/movieclips.