0

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

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);
    }
}

}

现在,在我的 main 中,我还使用了 Shared 的一个实例,如下所示:

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);
                }
            }
        }
    }

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

4

1 回答 1

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);
            }
        }
    }
    */
}

请多解释一下您的代码,我可以更新此答案以使其更相关。

于 2012-11-15T00:55:22.593 回答