2011-10-11 68 views
1

我正在使用Flash Actionscript 3.0中的橫向滾動遊戲。現在我試圖讓玩家通過觸摸某個物體來進入下一個等級。在下一個關卡加載之前,我會嘗試從舞臺中刪除所有先前關卡的資源(背景和「底部」),這些是MovieClip。當下一個級別加載時,我仍然可以看到上一級的資產,儘管玩家不能與它們進行交互。我的Actionscript 3.0舞臺不會清除

我看了看周圍的互聯網,並嘗試了一些方法來解決我的問題,但我無處可去。以下是我用於從一個級別轉換到另一個級別的代碼。 'bgContainer'只包含未加載的關卡的背景MovieClip,'allContainer'包含未關卡的關卡和其他環境對象。這些容器稍後將加載下一級所需的對象。

// Check if the Player has touched Level 1's end point 
    private function checkLevel1To2Collision(event:Event):void { 
     // Has the Player touched Level 1's end point? 
     if (HitTest.intersects(player, level1To2, this)) { 

       // Remove Level 1's Background from the stage 
       //stage.removeChild(bgContainer); 

       removeEventListener(Event.ENTER_FRAME, scrollScreen); 

       // Clear everything from the stage (only if there is already something on the stage) 
       //while (numChildren > 0) { 

        //for (var stgIndex = 0; stgIndex < stage.numChildren; stgIndex++) { 
         //stage.removeChildAt(0); 
        //} 
       //} 
       //} 

       stage.removeChild(allContainer); 
       stage.removeChild(bgContainer); 

       // Clear out all elements from the 'allContainer' before reloading the current level 
       for (var allIndex1:int = 0; allIndex1 < allContainer.numChildren; allIndex1++) { 
        allContainer.removeChildAt(allIndex1); 
        //if (stage.getChildAt(allIndex1)) { 
         //stage.removeChildAt(allIndex1); 
        //} 
       } 

       // Remove the elements within 'bgContainer' from the stage 
       for (var bgIndex1:int = 0; bgIndex1 < bgContainer.numChildren; bgIndex1++) { 
        bgContainer.removeChildAt(bgIndex1); 
        //if (stage.getChildAt(bgIndex1)) { 
         //stage.removeChildAt(bgIndex1); 
        //} 
       } 


       // Load Level 2 
       loadLevel2(event); 
     } 
    } // End of 'checkLevel1To2Collision' function 

可以看出,我已經嘗試了至少兩種技術來卸載上一級的資產。我已經嘗試過使用'for'循環逐個刪除所有元素。我試過在索引0處移除舞臺元素,而舞臺上有一個對象。我還嘗試使用'addChildAt()'添加對象時,引用'root'而不是'stage'。這些技術都沒有奏效。我仍然不知道爲什麼以前的級別不會被卸載。

任何幫助,將不勝感激!

謝謝!

+0

你試過this.removeChild,而不是stage.removeChild? –

+0

你可能會發布代碼添加元素到舞臺和容器的代碼嗎? –

+0

@AmyBlankenship我嘗試使用「this.removeChild」而不是「stage.removeChild」。但是這會導致錯誤,指出「this」不是顯示對象的子項。我只是試了一遍,但沒有做任何事情。 –

回答

1

如果您不確定allContainer的父,然後用

allContainer.parent && allContainer.parent.removeChild(allContainer.parent); 

(此左側只是作爲一個後衛,以確保右側被稱爲只有allContainer是在舞臺上,你可以或者把它寫成:)

if (allContainer.parent) 
{ 
    allContainer.parent.removeChild(allContainer.parent); 
} 

for循環你寫的也是有問題的,因爲你已經刪除了第一個孩子後0,孩子們都向下移動一個索引,所以孩子在1現在在0,但你的索引已經移到1,所以你不斷失蹤的孩子!取而代之的是:

while (allContainer.numChildren) 
{ 
    allContainer.removeChildAt(0); 
} 

這樣,while循環會一直循環,直到allContainer的所有子節點都被刪除。

另外,如果你想這對最佳快速運行,使用

var i:int = allContainer.numChildren; 
while (i--) 
{ 
    allContainer.removeChildAt(0); 
} 

希望這有助於。

+0

'while'循環清除容器沒問題,我想,它不會清除階段,在「while(stage.numChildren)」中,「編譯器抱怨引用我必須計算哪些對象被認爲是「空」 當程序加載時,我會在舞臺上添加「eventListeners」,這些eventListeners控制着玩家的移動和其他事情,我懷疑當我嘗試清除舞臺,這些聽衆被刪除,導致問題。 要解決這個問題,我嘗試添加聽衆'這',但沒有奏效。我將這些聽衆添加到舞臺以外的任何其他位置? –

+0

我不會在舞臺上運行它,有些情況下Flash播放器可能會將其他東西添加到舞臺上(例如,如果您全屏顯示,則會出現小警告)。我的建議 - 只需要添加一個Sprite到舞臺上,稱之爲「root」或其他東西,然後添加所有內容。不要添加其他任何內容到舞臺,將其他內容添加到根目錄。這樣你就可以確定舞臺上唯一的東西就是你添加的東西。 – alecmce

+0

進一步思考 - 我敢打賭,你正在從添加到舞臺上的Sprite中運行刪除腳本。你不能這樣做,因爲一旦這個孩子從舞臺上移開,它就會失去對舞臺的引用(所以它報告'舞臺'是空的,而不是舞臺的任何成員)。 – alecmce