0

我想从for循环内的基类创建一些实例。

我创建了一个 MovieClip 符号并喜欢它Export for ActionScript

我正在尝试从该基类中创建一些实例。基类名称是Ball_Base.

for (var counter:uint = 1; counter <= 10; i++) {
    var "ball_instance" + counter:Ball_Base = new Ball_Base();
}

它得到以下错误:

Syntax error: expecting identifier before "ball_instance".

请告诉我我在这里缺少什么。

4

2 回答 2

1

你的代码不正确。

您的实例中必须有一个关于名称的属性,例如

public class Ball_Base {
    private var _name:String;

    public function get name():String
    {
        return _name;
    }

    public function set name(value:String):void
    {
        _name = value;
    }
}

当您处于循环中时,您可以:

var myBall:Ball_Base = new Ball_Base();
myBall.name = "ball_instance" + counter;

最后,如果您需要将所有实例存储在内存中,请将它们添加到 ArrayCollection

var myArray:ArrayCollection = new ArrayCollection();
for (var counter:uint = 1; counter <= 10; i++) {
    var myBall:Ball_Base = new Ball_Base();
    myBall.name = "ball_instance" + counter;

    myArray.addItem(myBall);
}
于 2013-08-22T09:09:49.547 回答
1

如果我在上面的理解应该是:

for (var counter:uint = 1; counter <= 10; counter++) {
    var instanceOfClass:Ball_Base = new Ball_Base();
        instanceOfClass.someVar = "ball_instance" + counter; // Assign some name to existing var in class
    var varName:String = instanceOfClass.someVar; // save that name in var for further operation
}
于 2013-08-22T09:17:31.730 回答