Actionscript 3:强制程序等待,直到调用事件处理程序

发布于 2024-08-27 18:51:36 字数 1304 浏览 10 评论 0原文

我有一个 AS 3.0 类,它使用 URLRequest 加载 JSON 文件。

package {

    import flash.display.MovieClip;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.events.Event;

    public class Tiles extends MovieClip {

        private var mapWidth:int,mapHeight:int;
        private var mapFile:String;

        private var mapLoaded:Boolean=false;

        public function Tiles(m:String) {

            init(m);

        }

        private function init(m:String):void {

            // Initiates the map arrays for later use.
            mapFile=m;

            // Load the map file in.
            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, mapHandler);
            loader.load(new URLRequest("maps/" + mapFile));


        }

        private function mapHandler(e:Event):void {

            mapLoaded=true;
            mapWidth=3000;

        }

        public function getMapWidth():int {

            if (mapLoaded) {

                return (mapWidth);

            } else {

                return(-1);
            }

        }

    }

}

文件加载完成后,mapHandler 事件会对类属性进行更改,然后使用 getMapWidth 函数来访问这些属性。但是,如果 getMapwidth 函数在加载完成之前被调用,则程序将失败。

如何让类等到文件加载后才接受函数调用?

I have an AS 3.0 class that loads a JSON file in using a URLRequest.

package {

    import flash.display.MovieClip;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.events.Event;

    public class Tiles extends MovieClip {

        private var mapWidth:int,mapHeight:int;
        private var mapFile:String;

        private var mapLoaded:Boolean=false;

        public function Tiles(m:String) {

            init(m);

        }

        private function init(m:String):void {

            // Initiates the map arrays for later use.
            mapFile=m;

            // Load the map file in.
            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, mapHandler);
            loader.load(new URLRequest("maps/" + mapFile));


        }

        private function mapHandler(e:Event):void {

            mapLoaded=true;
            mapWidth=3000;

        }

        public function getMapWidth():int {

            if (mapLoaded) {

                return (mapWidth);

            } else {

                return(-1);
            }

        }

    }

}

When the file is finished loading, the mapHandler event makes changes to the class properties, which in turn are accessed using the getMapWidth function. However, if the getMapwidth function gets called before it finishes loading, the program will fail.

How can I make the class wait to accept function calls until after the file is loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-09-03 18:51:36
 private function mapHandler(e:Event):void {
              if (mapLoaded) {

                mapLoaded=true;
                 mapWidth=3000;
                return (mapWidth);

             } else {

           getMapWidth()

        }

}

        public function getMapWidth():int 
                return(-1);


        }

这可能会解决您的问题,为什么在不需要时在 getMapWidth 中检查它。

 private function mapHandler(e:Event):void {
              if (mapLoaded) {

                mapLoaded=true;
                 mapWidth=3000;
                return (mapWidth);

             } else {

           getMapWidth()

        }

}

        public function getMapWidth():int 
                return(-1);


        }

This might solve your problem, why are checking it in getMapWidth when there is no need of it.

无戏配角 2024-09-03 18:51:36

好吧,所以我想出了我需要做什么。问题出在我的主时间线上的代码:

trace(bg.getMapWidth());

我忘记了代码只在主时间轴上没有事件监听器的情况下执行一次,就像这样

stage.addEventListener(Event.ENTER_FRAME, n);

function n(e:Event):void
{
    trace(bg.getMapWidth());
}

现在每帧返回一次宽度,并且一切正常。感谢您的帮助 ;)

Okay, so I figured out what I needed to do. The problem was with the code on my main timeline:

trace(bg.getMapWidth());

I forgot that the code only executed once without an event listener on the main timeline, like this

stage.addEventListener(Event.ENTER_FRAME, n);

function n(e:Event):void
{
    trace(bg.getMapWidth());
}

Now the width is returned once per frame, and everything works properly. Thanks for your help ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文