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

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

问题描述

我试图在AS3中创建一个简单的菜单。有一个名为 startButton 的精灵,当按下它时,会调用函数 startGame ,就是这样!但是,并不那么容易。我正在使用flashdevelop IDE,所以我试图调用一个 loader 来获得一个 .png 图像文件 spite startButton 。但是,这是行不通的。没有错误消息,调试器只是不响应。任何帮助?这是两个文件的代码



主要代码:

 包{

//其他文件
导入菜单;

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 {

//游戏值
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);
//入口点
}

//函数开始游戏
public function startGame(evt:MouseEvent):void {
removeChild(Menu.startButton );

$ b $ //每60秒更新
public function update():void {
trace(Updated);




code


$ b <

 包{

//其他文件
导入主要;

导入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);

//初始化startButton的位图值
startButton.x =(Main.gameWidth / 2) - (startButton.width / 2);
startButton.y =(Main.gameHeight / 2) - (startButton.height / 2);


//创建startButton位图
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);


$ b $ / code $ / pre

顺便说一句,该加载程序在使用之前成功加载图像,以防万一图像需要更多的时间,并且会产生错误。

解决方案

问题是你滥用静态。所有的静态方法/属性都是在类本身之前初始化的。因此,静态可以接收值,但不能运行任何代码。运行代码必须在所有类都准备好后才能发生,而静态初始化的情况并非如此。在你的情况下,startButton和loader是正确创建的,但是下一行永远不会运行'loader.load'。

不要滥用静态,你显然试图使用静态使你的代码写作和生活更容易,但最后,因为你滥用它,你总是会有更多的问题。


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.

解决方案

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.

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

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