AS3 debugger stops responding while trying to load

2019-09-20 18:24发布

问题:

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

Main code:

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

    }

}

And Menu Image code:

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.

回答1:

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.