【问题标题】:Stage becomes null阶段变为空
【发布时间】:2012-07-19 10:47:28
【问题描述】:

我正在使用 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
    }
}

【问题讨论】:

  • 来自docs->"If a display object is not added to the display list, its stage property is set to null."
  • 我知道,但是我没有从我的显示列表中删除“游戏动画剪辑”,并且在这个动画剪辑中我尝试访问 stage.stageWidth。在“游戏影片剪辑”中,我使用“ADDED_TO_STAGE”和“REMOVED_FROM_STAGE”。
  • GameMC 是否会附加到您放置在舞台上的图书馆项目?如果是这样,那么如果您的时间线跳转到它没有出现的帧,它将自动从舞台上移除。
  • 不,不是。而且我在上面的代码中犯了一个错误,它不是扩展的 MovieClip,它是扩展的 Sprite。

标签: actionscript-3 flash


【解决方案1】:

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

【讨论】:

  • 你写的是正确的。但它没有回答“Maybe someone can help me to point me to a solution for this problem?”的问题。
  • 在“游戏动画剪辑”中,我使用“ADDED_TO_STAGE”和“REMOVED_FROM_STAGE”,我不会从我的舞台/显示列表/任何内容中删除“游戏动画剪辑”,并且在我尝试访问的这个动画剪辑中舞台。
  • 是的,最简单的方法是设置一个标志,如果 DisplayObject 是否在舞台上。
  • 用一些伪代码编辑了我的问题以便更好地理解。
  • 您不需要标志 - 只需检查 stage 引用是否为 null - 如果是,则该对象不在显示列表中。
【解决方案2】:

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

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]
    } 
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-06
    • 2018-10-29
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多