AS3 调试器在尝试使用 Loader 将图像加载到精灵中时停止响应 [英] AS3 debugger stops responding while trying to load image into sprite using Loader

查看:20
本文介绍了AS3 调试器在尝试使用 Loader 将图像加载到精灵中时停止响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 AS3 中创建一个简单的菜单.有一个名为 startButton 的精灵,按下它会调用 function startGame,就是这样!但是,没那么容易.我正在使用 flashdevelop IDE,所以我试图调用 loader 来获取 spite startButton 的 .png 图像文件.但是,它不起作用.没有错误消息,调试器只是不响应.有什么帮助吗?这是两个文件的代码

I'm trying to create a simple Menu in AS3. There is a sprite called startButton, which when pressed will call the function startGame, and that's it! But, not so easy. I'm using flashdevelop IDE so I'm trying to call a loader to get a .png image file for the spite startButton. But, it doesn't work. There are no error messages, the debugger just does not respond. Any help? Here is the code for both files

主要代码:

package {

    //Other Files
    import Menu;

    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;

    public class Main extends Sprite {

        //Game values
        public static var gameWidth:int = 750;
        public static var gameHeight:int = 750;

        public function Main() {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

            addChild(Menu.startButton);
            Menu.startButton.addEventListener(MouseEvent.CLICK, startGame);
            stage.addEventListener(Event.ENTER_FRAME, update);
        }

        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point
        }

        //Function starts game
        public function startGame(evt:MouseEvent):void {
            removeChild(Menu.startButton);
        }

        //Updates every 60 seconds
        public function update():void {
            trace("Updated");
        }

    }

}

和菜单图片代码:

package {

    //Other files
    import Main;

    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;

    public class Menu extends Sprite {

        public static function imageLoaded():void {
            startButton.addChild(loader);

            //initizlize values for startButton Bitmap
            startButton.x = (Main.gameWidth / 2) - (startButton.width / 2);
            startButton.y = (Main.gameHeight / 2) - (startButton.height / 2);
        }

        //create startButton Bitmap
        public static var startButton:Sprite = new Sprite();
        public static var loader:Loader = new Loader();
        loader.load(new URLRequest("lib/menustartbutton.png"));
        loader.addEventListener(Event.COMPLETE, imageLoaded);

    }
}

顺便说一句,我会等待加载器成功加载图像,然后再使用它,以防图像花费更多时间并绘制错误.

By the way, I wait for the loader to successfully load the image before working with it, just in case the image takes more time and it draws errors.

推荐答案

问题是你误用了静态.所有静态方法/属性都在类本身之前初始化.因此,静态可以接收值,但不能运行任何代码.运行代码必须在所有类都准备就绪后发生,而静态初始化时则不然.在您的情况下,正确创建了 startButton 和 loader,但下一行永远不会运行loader.load".

The problem is that you misuse static. all static methods/properties are initialized before the classes themselves. As a result static can receive values but they cannot run any code. Running code has to happen after all classes are ready to go which is not the case when static is initialized. In your case startButton and loader are created correctly but the next line never runs 'loader.load'.

不要滥用静态,您显然是在尝试使用静态来使您的代码编写和生活更轻松,但最终因为您滥用它,您最终会遇到更多问题.

Don't misuse static, you are obviously trying to use static to make you code writing and life easier but at the end because you are misusing it you will always end up with more problems.

这篇关于AS3 调试器在尝试使用 Loader 将图像加载到精灵中时停止响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆