PDA

View Full Version : Linking class files.



benny1989
December 8th, 2008, 01:07 PM
Hi, i'm pretty new AS3, and i'm having problems linking a class file to my main document class. The class I want to link is a sound based class, and has play,pause buttons aswell as a volume slider, it is also linked to a mp3 file using URLRequest. When linking the sound class file(SoundSlider.as) to my main class document(Portfolio.as) I get the following error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at SoundSlider$iinit()
at Portfolio$iinit()

When attempting to link the two classes, I first added the import SoundSlider; statement to my main class, I also declared and made an instance of it,


var _soundslider:SoundSlider = new SoundSlider;

I then added used addChild(_soundslider) to add it onto the stage. All the files are in the same folder. Any help would be greatly appreciated.

Many Thanks
benny

benny1989
December 8th, 2008, 05:34 PM
update:

I used the debugger, and it seems to not like this line found in the SoundSlider file;


stage.addEventListener(MouseEvent.MOUSE_UP,stopSli der);

any suggestions would be appreciated.

Benny

Sage_of_Fire
December 8th, 2008, 05:55 PM
If that line is called immediately when the class is instantiated (not called from the outside through a function), then it doesn't have access to the stage right away. The stage variable is defined only once the object is added to the stage, and that happens after you've already made an instance of the class.

You could apply the listener externally, through the document class, but that would be pretty cumbersome. You could also make an psuedo-constructor function in your class called init() or something like that, put all your setup code in that function, and trigger that function with an Event.ADDED_TO_STAGE listener created in the real constructor. Then, all the code that needs the stage variable can be triggered only once the SoundSlider is actually on stage.

sekasi
December 8th, 2008, 05:55 PM
Event.ADDED_TO_STAGE