0

如何在 ActionScript 中创建动态变量?

示例代码:

import windows.nwindow;

for(var num:int = 0; num< 2; num++)
            {
                this["nWin"+num] = new nwindow();
                this["nWin"+num].width = 320;
                this["nWin"+num].height = 200;
                this["nWin"+num].title="window" + num;
                this["nWin"+num].open();
            }

当我运行上面的代码时,它会发出这个错误:

Error #1056: Cannot create property nWin0 on MultiWindow.

那么,我如何在这种情况下使用动态变量呢?

4

1 回答 1

1

您可以使用字典来实现这一点。例如-

        var dict:Dictionary = new Dictionary;
        for(var num:int = 0; num< 2; num++)
        {
            var str:String = "nWin"+num;
            dict[str] = new nwindow();
            dict[str].width = 320;
            dict[str].height = 200;
            dict[str].title="window" + num;
            dict[str].open();
        }
于 2013-12-13T05:24:36.627 回答