我正在做一个游戏,最初我手动将英雄电影剪辑放在舞台上。现在,我将主条目添加到容器中,并将容器加载到 Main.as 构造函数中。
我收到此行的 1009 错误:
bulletOffset = 5 / _root.accuracy;
以下是英雄类的相关代码:
public class Hero extends MovieClip {
private var radius:Number;
//Bullet offset
private var bulletOffset:Number;
//Player variables
private var walkingSpeed:int = 3;
private var shootingRate:int = 120;
private var s:int;
//Making all of the items on the stage accessible by typing "_root.[ITEM]"
private var _root:MovieClip;
private var leftKeyDown:Boolean = false;
private var upKeyDown:Boolean = false;
private var rightKeyDown:Boolean = false;
private var downKeyDown:Boolean = false;
private var punchKeyDown:Boolean = false;
//Player states (shooting, attacking etc)
private var shooting:Boolean = false;
public function Hero()
{
addEventListener(Event.ADDED, beginClass);
}
private function beginClass(event:Event):void
{
//Determine the radius
radius = this.width - 8;
_root = MovieClip(root);
bulletOffset = 5 / _root.accuracy;
blablablabla
您正在收听错误的事件。Event.ADDED
添加到任何显示列表时都会触发。但是您需要等待Event.ADDED_TO_STAGE
,然后才能使用root
:
public function Hero()
{
addEventListener(Event.ADDED_TO_STAGE, beginClass);
}