views:

39

answers:

1

I am finding it hard to fugure out the reference system in AS 3.0.

this is the code i have (i have trimmed it down in order to find the problem but to no avail)

package rpflash.ui {

import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Stage;
import nowplaying;
import flash.text.TextField;

public class RPUserInterface extends Sprite{

    var np:nowplaying;

    public function RPUserInterface(){
        }

    public function init(){
        var np:nowplaying = new nowplaying();
        this.addChild(np)
        }

    public function updateplayer(xml:XML){
        var artist: String = xml.nowplaying.artist.toString();
        var title: String = xml.nowplaying.title.toString();
        trace("UI:update");
        trace(this.np);// this gives me a null reference
        }
}   
} 

and still i cannot access np!!! trace this.np gives me a null reference. i am not even trying to access it from a subling class. (btw i def want to know how to do that as well.)

+2  A: 

In your init() function, you are instantiating a local variable called np. Try this instead:

public function init() {
    // var np:nowplaying = new nowplaying();
    np = new nowplaying();
    this.addChild(np);
}

Also, make sure init() is getting called before updateplayer(). Hope that helps.

RJ Regenold
To expand more, the "var" keyword always creates a new variable in the current scope. In the code you posted, you used "var" to declare your variable twice - the first time, you declare it outside of any functions, which is the correct way to declare a class property. The second time, when you declare it again inside a function, you are making a new variable which is scoped to within that function. Your later attempts to reference the variable don't work because they are referencing the class property, which never got initialized.
fenomas