2012-11-14 158 views
0

這是我的問題:我有一個擴展Main類的Shared類。在我的Shared類中,我實例化其他類並將它們添加到此類。類AS3:實例化類的實例

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

我沒有得到任何錯誤,但沒有對象出現在舞臺上,我可以」沒有發現問題。我認爲我的問題是Shared類不擴展Sprite,但是接下來我將不得不將大量參數傳遞給Shared,有沒有什麼辦法可以繞過這個?所以,我仍然可以延長主機...

+2

在你提出新的問題之前,你應該接受你以前的答案:[1](http://stackoverflow.com/questions/13260618/actionscript3-moving-objects-of-type-movieclip),[2] (http://stackoverflow.com/questions/13000019/assigning-unique-identifiers-to-array-of-objects-in-actionscript3),[3](http://stackoverflow.com/questions/13326312/as3- import-classes-sharing-variables-classes) – Taurayi

+0

我認爲你需要研究變量作用域。所有這些變量來自哪裏:span,i,j,_x,_y,size,d,addl? – danjp

+1

我不認爲你想讓你的Shared類繼承自你的主類,如果從構造函數調用init函數,看起來你會有'Shared'對象的堆棧溢出。 – BadFeelingAboutThis

回答

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

請解釋一下你的代碼了一點,我可以更新這個答案更重要。

+0

事情是我不希望共享中的所有對象都添加到舞臺上。高於3我只想添加一次,sh.g是一個由小方塊組成的網格,所以我希望每次迭代都會添加一個sh.m,而只有在數組中找到索引時纔會添加sh.m。 – wedran