How to create dynamic vbox variable in actionscript

I need to create multiple vbox-es in for each loop. Now I want to do something like this.

formsArray ["vb" + counter] = new VBox; formsArray ["vb" + counter] .visible = true;

add shortcuts, etc.

I cannot get this to work. Does anyone know how to create dynamic variable names for my vbox-es?

thanks

0


a source to share


1 answer


First of all, to use an associative array, you need to use an object, not an array (you may have already done that, then it doesn't matter).

You can achieve what you want to do like this:

var vbox:VBox;
var formsArray:Object = new Object();
var counter:int = 0;

for each(<statement>)
{
    vbox = new VBox();
    formsArray[("vb" + counter.toString())] = vbox;
    counter++;
}

      

The default VBox visibility property is true

, so there is no need to explicitly set it.



Answer the additional question in the comments:

You really don't need to use dynamic links to do what you want to do. The best way to create a custom component for this is by extending the VBox class by creating a new MXML class with VBox as the root tag. Something like this:

<mx:VBox ... >

    <mx:Button ... click="btnClickHandler()"/>
    <mx:Script>
        <![CDATA[

            // Toggles visibility of the VBox
            private function btnClickHandler():void
            {
                 visible = !visible;
            }

        ]]>
    </mx:Script>

</mx:VBox>

      

Then you can just create as many of these custom VBox: es as you need. However, making the VBox invisible will make the contained button invisible and also make it difficult to click again. :) You probably want to refer to this. Anyway, I hope this points you in the right direction.

0


a source







All Articles