AS3:访问变量值

时间:2016-04-16 14:05:19

标签: actionscript actionscript-3

我在第3阶段的按钮上每个按钮都应该发出声音......我正试图

让课程代码动态化。但是我遇到问题请看代码。

我需要帮助! fla link& class link

我希望变量中的String不仅仅是名称。

package 
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class CustumClass extends MovieClip
    {
    private var R1:String ="im a string inside R1";
    private var R2:String ="im a string inside R2";
    private var R3:String  = "im a string inside R3";

    private var btns:Array;
    private var link:String;

        public function CustumClass()
        {
            // constructor code
            btns = new Array(r1,r2,r3);  //___ buttons on stage
            onLoop();

        }
        private function onLoop():void
        {
            for (var i:int = 0; i<btns.length; i++)
            {
                btns[i].addEventListener(MouseEvent.CLICK,handleBtn);
            }
        }

        private function handleBtn(e:MouseEvent):void
        {
            // ____ i want to get the string inside variable 

            link = e.target.name.toUpperCase();
            trace (link);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

而不是将您的按钮存储在Array

private var btns:Array;

使用Dictionary

private var buttons:Dictionary = new Dictionary();

然后将每个引用添加到一个按钮以及要与其关联的String

// constructor code

buttons[r1] = "text for r1";
buttons[r2] = "text for r2";
buttons[r3] = "text for r3";

onLoop();

您可以遍历密钥。

private function onLoop():void
{
    for (var button:Object in buttons)
    {
        button.addEventListener(MouseEvent.CLICK,handleBtn);

        // if above fails, try something like
        IEventListener(button).addEventListener(MouseEvent.CLICK,handleBtn);
    }
}

您可以使用键

检索值
private function handleBtn(e:MouseEvent):void
{
    trace (buttons[e.currentTarget]);
}