nikel
nikel

Reputation: 653

1120: Access of undefined property stage

I'm trying to make a class however I get these errors:

... \SubtitleLoader.as, Line 14, Column 8 1120: Access of undefined property stage. ... \SubtitleLoader.as, Line 20, Column 10 1061: Call to a possibly undefined method addEventListener through a reference with static type SubtitleLoader.

Here's my code:

package
{
    import flash.events.*;
    import flash.display.Stage;
    import flash.net.URLRequest;
    import flash.net.URLLoader;

    public class SubtitleLoader
    {
        private var str:String;

        public function init():void
        {
            if (stage)
            {
                LoadText();
            }
            else
            {
                this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            }
        }

        private function onAddedToStage(event:Event):void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            LoadText();
        }

        private function LoadText():void
        {
            var url:URLRequest = new URLRequest("aman.srt");
            var urlLoader:URLLoader = new URLLoader();
            urlLoader.load(url);
            urlLoader.addEventListener(Event.COMPLETE, onComplete);
        }

        private function onComplete(event:Event):void
        {
            trace(event.data);
        }
    }
}

How can I fix this.

Upvotes: 0

Views: 734

Answers (1)

user45623
user45623

Reputation: 621

You can't add events because your class doesn't extend EventDispatcher. Your class can't be added to the stage because it doesn't extend any display classes. You probably want to extend flash.display.Sprite (which also extends EventDispatcher):

package
{
    import flash.events.*;
    import flash.display.Sprite; //import sprite
    import flash.display.Stage;
    import flash.net.URLRequest;
    import flash.net.URLLoader;

    public class SubtitleLoader extends Sprite //extend sprite, inheriting EventDispatcher as well
    {

Also, it is bad coding style to give your function names UpperCamelCase (LoadText). UpperCamelCase is reserved for class names.

Upvotes: 1

Related Questions