我正在尝试创建一个系统,将每个块放在一个块数组中,我可以使用它通过 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 中执行此操作的更好方法的建议。
谁能解释为什么会这样?