在一个没有意义的动画片段上出现1009错误

时间:2014-03-13 17:54:21

标签: actionscript-3 null runtime-error containers stage

我正在制作游戏,最初我手动将英雄动画片段放在舞台上。现在我将英雄添加到容器中并在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

1 个答案:

答案 0 :(得分:1)

你正在听错事件。将Event.ADDED添加到任何显示列表时会被触发。但是,您需要在Event.ADDED_TO_STAGE之前等待root

public function Hero() 
{
    addEventListener(Event.ADDED_TO_STAGE, beginClass);
}