简体   繁体   English

AS3类可以创建自己的实例吗

[英]Can a AS3 class create instances of itself

Would this work? 这行得通吗?

From the start of the game it creates a box sprite and this triggers a custom event. 从游戏开始,它会创建一个盒子精灵,并触发自定义事件。

If I use public static functions and some event listener such as this quasi-code: 如果我使用公共静态函数和某些事件侦听器(例如此准代码):

 public static function tile()
 {
 if this hears statusbox.statuschanged 

and this triggers a static private function 
 } 

static private function 
create instances of tile 
with 
public function tile(id:uint,... ) 

There's no reason that a given class can't create instances of itself. 给定的类没有理由不能创建其自身的实例。 You would just have to ensure that it didn't do so in any method called from its own constructor, or in any automatically instantiated class variables. 您只需要确保它在从其自己的构造函数调用的任何方法或任何自动实例化的类变量中都没有这样做。 Otherwise you would create a non-terminating loop! 否则,您将创建一个非终止循环! It's rather difficult to read your quasi code, but it sounds like you wouldn't be creating any inner references upon instantiation, so you'll probably be in the clear. 读取准代码相当困难,但是听起来您在实例化时不会创建任何内部引用,因此您可能会很清楚。 To give some examples anyway: 无论如何要举一些例子:

Will work: 将工作:

package {

    public class Tile {

        private var _innerTile:Tile;

        public function Tile() {

        }

        public function innerTile_create():Tile {
            _innerTile = new Tile();
            return _innerTile;
        }

    }

}

Won't work: 无法运作:

package {

    public class Tile {

        public function Tile() {
            var innerTile:Tile = new Tile();
        }

    }

}

Also won't work 也行不通

package {

    public class Tile {

        private var _innerTile:Tile = new Tile();

        public function Tile() {

        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM