AS3 - TypeError:错误#1009

时间:2013-09-24 12:07:06

标签: actionscript-3 exception nullreferenceexception typeerror

我正在尝试创建一个系统,将每个块放在一个块数组中,我可以使用它通过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。我愿意接受有关在AS3中使用XML文件中定义的块ID更好地实现此目的的建议。

任何人都可以解释为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

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