我正在使用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'。這些技術都沒有奏效。我仍然不知道爲什麼以前的級別不會被卸載。
任何幫助,將不勝感激!
謝謝!
你試過this.removeChild,而不是stage.removeChild? –
你可能會發布代碼添加元素到舞臺和容器的代碼嗎? –
@AmyBlankenship我嘗試使用「this.removeChild」而不是「stage.removeChild」。但是這會導致錯誤,指出「this」不是顯示對象的子項。我只是試了一遍,但沒有做任何事情。 –