-1

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.

Elgin Beloy
  • 93
  • 1
  • 6
  • Put braces around your `if`-`else` blocks. – Brian Dec 14 '15 at 16:01
  • One, it's default for their not to be brackets on those specific statements. Two, It's perfectly fine for your if statement functionality to not have brackets, it will cause no errors. It's not even necessarily considered better. Please only Comment if it's helpful to my problem. – Elgin Beloy Dec 14 '15 at 23:51
  • It's more error-prone to omit the braces. Maybe tomorrow you need to add a second event listener in the `else` case -- oops, you forgot to add the braces; now it gets added whether it's needed or not. As to not commenting...you're the one who posted your problem to a public website. Don't complain that the responses aren't to your liking. – Brian Dec 15 '15 at 16:43

1 Answers1

2

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.

BotMaster
  • 2,233
  • 1
  • 13
  • 16