开发者

How do you pass an Array from the timeline to a class in as3?

I have an array in a timeline that I need to pass t开发者_如何学JAVAo a class file, but I can't seem to figure out how to do this, as the method of inter-class array transfers doesn't seem to work.


You can always pass an array as a method parameter or set a public variable.
In my opinion, the best way to do this is using a setter method:

class Foo {

    private var _bar:Array;

    public function set bar ( arr : Array ) : void {
        _bar = arr;
    }

    // it is customary to provide a getter method to retrieve the value later

    public function get bar ( ): Array ) {
        return _bar;
    }

    // more stuff happening here.
}

You would call it from a frame action like this:

var foo:Foo = new Foo();
foo.bar = [ 1,2,3 ];

There are some terminology quirks in your question, so this is to clarify things:

A class is to an object as a blueprint is to a house. When you set property values, you usually set these on an object ( an instance of the class ), rather than on the class itself. That way, each object instance can have different values. A class can have variables, too. These are called "static" variables, and they are the same for all instances of the class. Let's assume _bar were declared private static var _bar: If you would then create var foo2:Foo = new Foo();, foo2.bar would automatically return the same value as foo.bar, and if you changed the value of foo2.bar, you would also alter the value of foo.bar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜