-1

我正在尝试创建一个系统,将每个块放在一个块数组中,我可以使用它通过 XML 轻松地将块添加到舞台,但是我得到了

[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.

当它初始化'blockStone'时。

这是我的主要 Block 类,每个块都在这里初始化。

package com.snakybo.platformengine.block {
    import flash.display.MovieClip;

    public class Block extends MovieClip {
        public static var blockList:Array = [];

        public static const blockStone:Block = (new BlockStone(0));

        public var blockID:int;

        private var mc:MovieClip;

        public function Block(blockID:int, mc:MovieClip) {
            if (blockList[blockID] != null) {
                throw new Error("Slot " + blockID + " is already occupied by " + blockList[blockID] + " when adding " + this);
            } else {
                blockList[blockID] = this;

                this.blockID = blockID;
                this.mc = mc;

                mc.x = 100;
                mc.y = 100;

                addChild(mc);
            }
        }
    }
}

这是 BlockStone 类:

package com.snakybo.platformengine.block {

    public class BlockStone extends Block {
        public function BlockStone(blockID:int) {
            super(blockID, new stone());
        }
    }
}

FlashDevelop 在出错时引用此行:

public class BlockStone extends Block {

这是堆栈跟踪:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at global$init()[C:\Users\Kevin\Desktop\Extra\Code\Actionscript\PlatformEngine\src\com\snakybo\platformengine\block\BlockStone.as:3]
    at com.snakybo.platformengine.block::Block$cinit()
    at global$init()[C:\Users\Kevin\Desktop\Extra\Code\Actionscript\PlatformEngine\src\com\snakybo\platformengine\block\Block.as:5]
    at com.snakybo.platformengine::Game()[C:\Users\Kevin\Desktop\Extra\Code\Actionscript\PlatformEngine\src\com\snakybo\platformengine\Game.as:13]
    at com.snakybo.platformengine::Main/init()[C:\Users\Kevin\Desktop\Extra\Code\Actionscript\PlatformEngine\src\com\snakybo\platformengine\Main.as:20]
    at com.snakybo.platformengine::Main()[C:\Users\Kevin\Desktop\Extra\Code\Actionscript\PlatformEngine\src\com\snakybo\platformengine\Main.as:11]

我很确定这是 AS3 的问题,因为这种方法在 Java 中有效。我愿意接受有关使用 XML 文件中定义的块 ID 在 AS3 中执行此操作的更好方法的建议。

谁能解释为什么会这样?

4

1 回答 1

0

看起来您在通过 Block 构造函数之前实例化了一个新的 Blockstone。尝试简单地声明 public static const blockStone:Block; 无需先为其设置值,然后在 Block 的构造函数中设置它。

于 2013-09-24T12:19:52.097 回答