Eli Stone
Eli Stone

Reputation: 1555

This, stage & parent are null

Im trying to access the stage from an external class this is what I have:

Player.as (Main Class)

import flash.display.MovieClip;

public class Player extends MovieClip 
{

    private var _controls:Controls;

    public function Player()
    {
        // constructor code
        _controls = new Controls();
    }
}

Controls.as

import flash.display.MovieClip;

public class Controls extends MovieClip
{
    private var _playbtn:MovieClip;

    public function Controls()
    {
        trace(this.getChildByName("playbtn"));
    }

}

but this line trace(this.getChildByName("playbtn")); always errors I have even tried:

trace(stage.getChildByName("playbtn"));
trace(parent.getChildByName("playbtn"));

But I get the same error:

Null for this and

Cannot access a property or method of a null object reference. for stage & parent

Is there something I am doing wrong?

Upvotes: 0

Views: 485

Answers (3)

Fygo
Fygo

Reputation: 4675

If you are adding everything to the stage manually, there is no need to create a new instance of Controls in the constructor of Player, you should just retrieve the reference:

_controls = this.controls; //where 'controls' is the instance name you assigned in Flash IDE.

Also, where is the 'playbtn' instace you are trying to retrieve? Is it on the stage or is inside of the Controls instance? As you are tracing this.getChildByName("playbtn"); it HAS TO be inside of Controls with the instance name playbtn assigned in the IDE (or via .name property).

You are getting indeed a null for parent and stage because you have not added the instance to the stage. What you are doing right now:

  1. You instantiate Player, the player then instantiates the Controls (it does not retrieve the reference you have on stage, because you are calling it with the new keyword!)
  2. The new controls have never been added to anything, therefore they have no parent and neither stage.

Upvotes: 0

Josh
Josh

Reputation: 8159

I think you are mistaken as to what getChildByName actually does. It returns reference to an object with its name property set to the supplied value. It does not return a reference to an object set to a variable with the supplied name.

For a reference to be returned by getChildByName, you must instantiate an object, set its name to something, and then call addChild on a DisplayObjectContainer with that object. Then you can call getChildByName on the DisplayObjectContainer.

For future reference, stage will be null until the object is added to the stage. parent will be null until the object is used in an addChild call

Upvotes: 1

moosefetcher
moosefetcher

Reputation: 1901

Display objects get access to their 'stage' property when they are added to the stage. You haven't added the _controls to the stage ( eg addChild(_controls) ) when you try to access the stage property.
Add an event listener (Event.ADDED_TO_STAGE) to the constructor of your Controls class that points to a handler that then checks the stage property.

Upvotes: 1

Related Questions