找到你要的答案

Q:1120: Access of undefined property stage

Q:1120:访问未定义的属性阶段

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.

我试图做一个班,但我得到这些错误:

... \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.

这是我的密码:

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);
        }
    }
}

我怎样才能修复这个。

answer1: 回答1:

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.

你不能因为你添加事件类不延长EventDispatcher。不能将类添加到平台,因为它不会扩展任何显示类。你可能想延长flash.display.sprite(这也延伸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
    {

此外,它是不好的编码风格给你的函数名UpperCamelCase(loadtext)。uppercamelcase保留类名。

actionscript-3  flash-cc