0

我正在使用 Flash Professional CS5.5 中的 AS3 创建游戏。

在这个游戏中,我有一个“再次”按钮,以便玩家可以重置关卡并从新开始。我现在的问题是:

单击“再次”后,舞台变为空。

我在“ResetLevel”方法中所做的只是将某些元素的 x 和 y 位置设置回 0,从影片剪辑中删除一些项目,但我不会从显示列表中删除所有项目。所以背景,平视显示器,飞机并没有从影片剪辑中删除。这是我的显示列表的草图。可移除项目有时为零,有时为 30 或更多项目(取决于游戏时间,依此类推)

Displaylist:

stage
|-- Game movieclip
    |--LevelBackground
    |--Removeable item 
    |--Removeable item
    |--Removeable item
    |--Plane
    |--HUD

但是在删除“可移动项目”并设置 levelbackground 和 plane 的位置坐标后,舞台为空。

也许有人可以帮我指出这个问题的解决方案。

编辑:

“ResetLevel”方法将在“游戏动画剪辑”中调用,并且舞台也将从“游戏动画剪辑”访问。因此,当我重置关卡时,我不会从显示列表中删除“游戏动画剪辑”。我只删除了游戏影片剪辑中包含的一些元素。

这里有一些来自“游戏影片剪辑类”(GameMC)的伪代码:

public class GameMC extends Sprite {

    //Some properties here

    public function GameMC() {
        //Some code here

        //--Events--
        this.addEventListener(Event.ADDED_TO_STAGE, Init, false, 0, true);
        this.addEventListener(Event.REMOVED_FROM_STAGE, Removed, false, 0, true);
    }

    private function Init(e:Event) {
        this.removeEventListener(Event.ADDED_TO_STAGE, Init);
        //Some Code here
    }

    private function ResetLevel() {
        //Some Code here, too
        if(removeItemArray.length > 0) {
            for(i = 0; i < removeItemArray.length; i++) {
                currentRemoveableItem = removeItemArray[i];
                this.removeChild(currentRemoveableItem );
                removeItemArray.splice(i, 1);
            }
        }
        level.x = 0;
        level.y = 0;

        trace(stage); //Will output null
    }
}
4

2 回答 2

2

当从 DisplayList 中删除 DisplayObject 时,它不再持有对舞台的任何引用。因此,无论您需要设置/计算什么,都应在有效状态下进行。Event.ADDED、Event.ADDED_TO_STAGE、Event.REMOVED 和 Event.REMOVED_FROM_STAGE 有助于验证 DisplayObject 的状态是否有效。

于 2012-07-19T10:57:13.247 回答
0

现在我曾经将舞台存储到一个属性中并访问它:

public class GameMC extends Sprite { 

    //Some properties here
    private var stagevar:Stage;

    public function GameMC() { 
        //Some code here 

        //--Events-- 
        this.addEventListener(Event.ADDED_TO_STAGE, Init, false, 0, true); 
        this.addEventListener(Event.REMOVED_FROM_STAGE, Removed, false, 0, true); 
    } 

    private function Init(e:Event) { 
        this.removeEventListener(Event.ADDED_TO_STAGE, Init); 
        this.stagevar = stage;
        //Some Code here 
    } 

    private function ResetLevel() { 
        //Some Code here, too 
        if(removeItemArray.length > 0) { 
            for(i = 0; i < removeItemArray.length; i++) { 
                currentRemoveableItem = removeItemArray[i]; 
                this.removeChild(currentRemoveableItem ); 
                removeItemArray.splice(i, 1); 
            } 
        } 
        level.x = 0; 
        level.y = 0; 

        trace(stagevar); //Will output [Object Stage]
    } 
} 
于 2012-07-19T15:59:43.107 回答