PDA

View Full Version : Accessing constructor variable from a function?



Cine
July 23rd, 2008, 11:30 AM
So, I'm trying to write a generic video player in AS3. I actually had it working perfectly when writing it on the stage in a frame. (so much easier!) Then I decided to convert it to a document class, that being a better coding practice and all.

My main issue now is this part in the constructor:


// Create net connection and stream.
var conn:NetConnection = new NetConnection();
conn.connect(null);

var stream:NetStream = new NetStream(conn);
stream.play(mfile);

var metaListener:Object = new Object();
metaListener.onMetaData = theMeta;
stream.client = metaListener;

// Sound
var st:SoundTransform = new SoundTransform();
stream.soundTransform = st;

// Video
var video:Video = new Video();
video.attachNetStream(stream);


This is followed by a bunch of eventListeners. Several functions called by the eventListeners reference conn, stream and str, so the result is a whole bunch of 1120: Access of undefined property conn. errors. This is where I need the variables to go global.

I did attempt to move the variables above out of the function, but that gave me the following output, which leaves me even more lost.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at src.videoPlayer::videoPlayer()

Any help is deeply appreciated!

senocular
July 23rd, 2008, 11:37 AM
Generally you would declare all of your variables in the class body (the code block following "class"). Then, in your constructor you would define those variables with their actual values. Because this is done in the constructor, all values should be valid for any other process that needs them since the constructor is the first thing that runs when an instance is created.

So basically do everything in your constructor that you had in your first frame (asside from functions, they are separate in the class body, not in the constructor) except put all the var [variable]:[Type] declarations in the class body and change all the uses of var [variable]:[Type] in your definition code to just be [variable].

Cine
July 23rd, 2008, 11:47 AM
Oh, that makes a lot of sense. Thank you!

Dammit, though! The "TypeError: Error #1009: Cannot access a property or method of a null object reference. at src.videoPlayer::videoPlayer()" error is back now, and it really tells me absolutely nothing. I've tried googling around, but can't spot any relevant solutions. Ugh.

senocular
July 23rd, 2008, 12:12 PM
That's a tough error to track down since all it does is tell you what function its occuring in.

Basically anywhere you do "something.somethingelse" the error can occur if "something" has not yet been defined or is null.

The easiest way to track it down is to use break points throughout that method and test using debug or just throw a bunch of trace statements throught the code block and see which the last trace was before the error occurred. The code below that will be the offending script