0

これが私の問題です。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);
    }
}

}

今、私のメインでは、次のように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に大量の引数を渡す必要があります。これを回避する方法はありますか?だから私はまだメインを拡張することができます...

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 に答える