Question

I'm making a game, and originally I placed the hero movieclip on the stage manually. Now I add the hero to a container and load the container in the Main.as constructor.

I get a 1009 error for this line:

bulletOffset = 5 / _root.accuracy;

Here is the relevant code of the hero class:

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
Was it helpful?

Solution

You are listening to the wrong event. Event.ADDED will be fired when it's added to any display list. But you need to wait for Event.ADDED_TO_STAGE before root will be available to you:

public function Hero() 
{
    addEventListener(Event.ADDED_TO_STAGE, beginClass);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top