从舞台上删除影片剪辑
我正在使用 Flash Actionscript 3.0 开发一款横向卷轴游戏。现在我试图让玩家通过触摸某个物体来进入下一个级别。在加载下一个关卡之前,我尝试从舞台上删除所有上一个关卡的资源(背景和“地板”),即 MovieClip。当下一个关卡加载时,我仍然可以看到上一个关卡的资产,尽管玩家无法与它们交互。
我在互联网上浏览并尝试了一些方法来解决我的问题,但我一无所获。以下是我用来从一个级别转换到下一个级别的代码。 “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”。这些技术都不起作用。我还是不明白为什么之前的关卡不会被卸载。
任何帮助将不胜感激!
谢谢!
I am working on a side-scrolling game in Flash Actionscript 3.0. Right now I am trying to allow the Player to advance to the next level by touching a certain object. Before the next level is loaded, I try to remove all of the previous level's assets (background and "floor"), which are MovieClips, from the stage. When the next level loads, I can still see the previous level's assets, although the Player cannot interact with them.
I have looked around the Internet and tried a few methods to fix my problem, but I have gotten nowhere. Following is the code I use to transition from one level to the next. the 'bgContainer' contains only the unloaded level's background MovieClip and the 'allContainer' contains the unloaded level's floor and other environmental objects. These containers are later loaded with the objects required in the next level.
// 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
As can be seen, I have tried at least two techniques to the unload the previous level's assets. I have tried going through the stage and removing all elements one by one using a 'for' loop. And I have tried removing the stage element at index 0 while the stage has an object on it. I also tried referring to 'root' instead of 'stage' while adding objects using 'addChildAt().' None of those techniques worked. I still have no idea why the previous level will not be unloaded.
Any help would be appreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你不确定 allContainer 的父级是什么,那么使用
(这个左侧只是充当一个守卫,以确保只有当 allContainer 在舞台上时才调用右侧,你也可以将其写为
:)你写的-loop也是有问题的,因为在你删除了0处的第一个孩子之后,孩子们都向下移动了一个索引,所以1处的孩子现在在0处,但你的索引已经移动到1,所以你一直缺少孩子!而是使用这个:
这样 while 循环将继续循环,直到 allContainer 的所有子级都被删除。
或者,如果您希望它以最佳速度快速运行,请使用
希望这会有所帮助。
If you are unsure what the parent of allContainer is, then use
(this left-side just acts as a guard to ensure that the right-side is called only if allContainer is on the stage, you could alternatively write it as:)
The for-loop you write is also problematic, because after you've deleted the first child at 0, the children all shift down one index, so the child at 1 is now at 0 but your index has moved to 1, so you keep missing children! Instead use this:
This way the while loop will keep looping around until all the children of allContainer are removed.
Alternatively, if you want this to run optimally quickly, use
Hope this helps.