AS3:实例化类的实例

时间:2012-11-14 23:18:11

标签: actionscript-3 instance extending-classes

这是我的问题:我有一个扩展Main类的Shared类。在我的Shared类中,我正在实例化其他类并将它们添加到此类中。班级

package 
{
import flash.display.Sprite;
public class Shared extends Main
{
    public var m:Mirror = new Mirror(span, i*span+j, _x, _y, size, d);
    public var g:Grid = new Grid(i*span+j,size, addl, span, _x, _y);
    public var b:Ball = new Ball(30); 
    public var mc:MovieClips = new MovieClips(30, _x, _y, size, span); 
    public var l:Level = new Level(levelCount); 
    public function Shared(){
        this.addChild(m); 
        this.addChild(g);
        this.addChild(b);
        this.addChild(mc);
        this.addChild(l);
    }
}

}

现在,在我的主要内容中我也使用了共享实例:

private function init(e:Event = null):void 
    {
        var sh:Shared = new Shared();

        stage.addChild(sh.b);
        stage.addChild(sh.mc);
        stage.addChild(sh.l);

        for (i = 0; i < span; i++) 
        {
           for (j = 0; j < span; j++) 
           {
               stage.addChild(sh.g);
               if(addl.indexOf(i*span+j)==true){
                    stage.addChild(sh.m);
                }
            }
        }
    }

我没有收到任何错误,但没有任何对象出现在舞台上,我无法找到问题。我认为我的问题是共享类没有扩展Sprite,但是我必须向Shared传递大量的参数,有没有办法绕过这个?所以我仍然可以扩展主...

1 个答案:

答案 0 :(得分:1)

我认为您不希望自己的Shared类继承自Main类,请记住在课后使用extends Main,继承其中的所有代码,这意味着你继承了创建Shared类实例的函数,这可能导致堆栈溢出。

扩展类与获取类的实例不同。主类的所有i / j / size等属性都是一个完全独立的实例,而不是继承Main的Shared类。

这可能是你想要做的:

public class Shared extends Sprite { //extend Sprite instead of Main
    public var m:Mirror; //don't instantiate these objects yet, just declare them
    public var g:Grid;
    public var b:Ball; 
    public var mc:MovieClips; 
    public var l:Level; 

    public function Shared(main:Main){  //pass in a reference to your main class instance
        //instanciate these objects in your constructor
        m = new Mirror(main.span, main.i*main.span+main.j, main._x, main._y, main.size, main.d); //all these vars (span, i,j,_x,_y, size, d, addl) are not defined anywhere that you've shown, I'm assuming they are part of your main class
        g = new Grid(main.i*main.span+main.j,main.size, main.addl, main.span, main._x, main._y);
        b = new Ball(30); 
        mc = new MovieClips(30, main._x, main._y, main.size, main.span); 
        l = new Level(main.levelCount); 

        this.addChild(m); 
        this.addChild(g);
        this.addChild(b);
        this.addChild(mc);
        this.addChild(l);
    }
}

在你的主要课程中:

private function init(e:Event = null):void 
{
    var sh:Shared = new Shared(this); //pass a reference to this Main class instance

    //stage.addChild(sh.b); // just add the Shared instance to the stage directly
    //stage.addChild(sh.mc);
    //stage.addChild(sh.l);

    addChild(sh); //you only need to add the Shared class instance, since you've already added those other objects in the Shared class

    /* no idea what your trying to do here, but I don't think it's what you're expecting
    for (i = 0; i < span; i++) 
    {
       for (j = 0; j < span; j++) 
       {
           stage.addChild(sh.g);
           if(addl.indexOf(i*span+j)==true){
                stage.addChild(sh.m);
            }
        }
    }
    */
}

请详细解释您的代码,我可以更新此答案以使其更具相关性。