ActionScript 3:LoaderInfo COMPLETE 事件在 load() 和 loadBytes() 之后不会触发

发布于 2024-12-05 10:03:31 字数 3914 浏览 1 评论 0原文

我正在尝试使用带有 Loader 对象的 ActionScript 加载 PNG 图像。这对于某些图像效果很好(INIT 和 COMPLETE 事件按预期触发),而对于其他图像则不然。我在这个帖子中读到 URLLoader 可能会有所帮助,所以我尝试了,之后使用 loadBytes() 函数。仍然不起作用:URLLoader 触发 COMPLETE 事件,但 LoaderInfo 对象不会。

我编写了一个示例类,它演示了两个文件的问题(一个有效,另一个无效)。

public class LoaderTest extends MovieClip {
    var output:TextField;
    var loader:Loader;
    var urlLoader:URLLoader;

    function LoaderTest() {
        output = new TextField();
        output.width = 1000;
        output.height = 1000;
        output.multiline = true;
        addChild(output);

        var t1:Timer = new Timer(0, 0);
        t1.addEventListener(TimerEvent.TIMER, function() {
            t1.stop(); loadMapDirect("map_in_big.png");
        });

        var t2:Timer = new Timer(1000, 0);
        t2.addEventListener(TimerEvent.TIMER, function() {
            t2.stop(); loadMapDirect("map_us_big.png");
        });

        var t3:Timer = new Timer(2000, 0);
        t3.addEventListener(TimerEvent.TIMER, function() {
            t3.stop(); loadMapBytes("map_in_big.png");
        });

        var t4:Timer = new Timer(3000, 0);
        t4.addEventListener(TimerEvent.TIMER, function() {
            t4.stop(); loadMapBytes("map_us_big.png");
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }

    function loadMapBytes(url:String):void {
        try {
            urlLoader = new URLLoader();
            urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            urlLoader.addEventListener(ProgressEvent.PROGRESS, progressListener);
            urlLoader.addEventListener(Event.COMPLETE, completeListenerBytes);

            output.appendText("\nLoading '"+url+"' with URLLoader ");
            urlLoader.load(new URLRequest(url));
        } catch (error:Error) {
            output.appendText("Err: " + error.message + "\n");
        }

    }

    function completeListenerBytes(e:Event):void {
        output.appendText("COMPLETE Event fired for URLLoader!\n");

        try {
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);
            output.appendText("Loading bytes with Loader ");
            loader.loadBytes(e.target.data as ByteArray);
        } catch (error:Error) {
            output.appendText("Err: " + error.message + "\n");
        }
    }

    function loadMapDirect(url:String):void {
        try {
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);

            output.appendText("\nLoading '"+url+"' with Loader ");
            loader.load(new URLRequest(url));
        } catch (error:Error) {
            output.appendText("Err: " + error.message + "\n");
        }
    }

    function completeListenerDirect(e:Event):void {
        var bmd:BitmapData = Bitmap(e.target.loader.content).bitmapData;
        output.appendText("COMPLETE Event fired for Loader! => h: " +  bmd.height + ", w: " + bmd.width + "\n");
    }

    function progressListener (e:ProgressEvent):void{
        output.appendText(".");
        if (e.bytesLoaded == e.bytesTotal) {
            output.appendText(" progress complete, " + e.bytesTotal + " bytes loaded!\n");
        }
    }
}

所有图像都是使用 PHP GD 库生成的,我使用 SWFTools 的 as3compile 进行编译。

您可以在 http://www.wichte-sind 上查看脚本的运行情况-wichtig.de/as3loader/loaderTest.swf

两张图片map_in_big.png和map_us_big.png在同一个文件夹中(不允许发布更多超链接)。

有什么想法吗?

I'm trying to load PNG images with ActionScript with a Loader object. This works fine for some of the images (the INIT and COMPLETE events are fired as expected), for some other it doesn't. I've read in this thread that a URLLoader might help, so I tried that, using the loadBytes() function afterwards. Still doesn't work: the URLLoader fires the COMPLETE event, but the LoaderInfo object does not.

I've written a sample class that demonstrates the problem with two files (one working, the other one not).

public class LoaderTest extends MovieClip {
    var output:TextField;
    var loader:Loader;
    var urlLoader:URLLoader;

    function LoaderTest() {
        output = new TextField();
        output.width = 1000;
        output.height = 1000;
        output.multiline = true;
        addChild(output);

        var t1:Timer = new Timer(0, 0);
        t1.addEventListener(TimerEvent.TIMER, function() {
            t1.stop(); loadMapDirect("map_in_big.png");
        });

        var t2:Timer = new Timer(1000, 0);
        t2.addEventListener(TimerEvent.TIMER, function() {
            t2.stop(); loadMapDirect("map_us_big.png");
        });

        var t3:Timer = new Timer(2000, 0);
        t3.addEventListener(TimerEvent.TIMER, function() {
            t3.stop(); loadMapBytes("map_in_big.png");
        });

        var t4:Timer = new Timer(3000, 0);
        t4.addEventListener(TimerEvent.TIMER, function() {
            t4.stop(); loadMapBytes("map_us_big.png");
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }

    function loadMapBytes(url:String):void {
        try {
            urlLoader = new URLLoader();
            urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            urlLoader.addEventListener(ProgressEvent.PROGRESS, progressListener);
            urlLoader.addEventListener(Event.COMPLETE, completeListenerBytes);

            output.appendText("\nLoading '"+url+"' with URLLoader ");
            urlLoader.load(new URLRequest(url));
        } catch (error:Error) {
            output.appendText("Err: " + error.message + "\n");
        }

    }

    function completeListenerBytes(e:Event):void {
        output.appendText("COMPLETE Event fired for URLLoader!\n");

        try {
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);
            output.appendText("Loading bytes with Loader ");
            loader.loadBytes(e.target.data as ByteArray);
        } catch (error:Error) {
            output.appendText("Err: " + error.message + "\n");
        }
    }

    function loadMapDirect(url:String):void {
        try {
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);

            output.appendText("\nLoading '"+url+"' with Loader ");
            loader.load(new URLRequest(url));
        } catch (error:Error) {
            output.appendText("Err: " + error.message + "\n");
        }
    }

    function completeListenerDirect(e:Event):void {
        var bmd:BitmapData = Bitmap(e.target.loader.content).bitmapData;
        output.appendText("COMPLETE Event fired for Loader! => h: " +  bmd.height + ", w: " + bmd.width + "\n");
    }

    function progressListener (e:ProgressEvent):void{
        output.appendText(".");
        if (e.bytesLoaded == e.bytesTotal) {
            output.appendText(" progress complete, " + e.bytesTotal + " bytes loaded!\n");
        }
    }
}

All images were generated with the PHP GD library and I'm compiling with SWFTools's as3compile.

You can see the script it in action on http://www.wichte-sind-wichtig.de/as3loader/loaderTest.swf

The two images map_in_big.png and map_us_big.png are in the same folder (not allowed to post more hyperlinks).

Any ideas?

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

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

发布评论

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

评论(1

很糊涂小朋友 2024-12-12 10:03:31

问题是您的应用程序可能是为 Flash Player 9 编译的。在版本 9 中,允许的最大图像尺寸为 2880 x 2800,map_us_big.png 为 3150 x 1570。当我为 Flash Player 10 编译该应用程序时,我成功运行了该应用程序。

以下是参考文献http://help.adobe .com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#BitmapData%28%29

在 AIR 1.5 和 Flash Player 10 中,BitmapData 的最大大小
对象的宽度或高度为 8,191 像素,并且对象的总数
像素不能超过 16,777,215 像素。 (因此,如果 BitmapData 对象是
宽 8,191 像素,高只能为 2,048 像素。)在 Flash Player 中
9 及更早版本和 AIR 1.1 及更早版本,限制为 2,880 像素
高度为 2,880 像素,宽度为 2,880 像素。如果指定宽度或高度
值大于 2880,则不会创建新实例。

The problem is that your application is probably compiled for Flash Player 9. In version 9 the maximum allowed image dimensions are 2880 x 2800 and map_us_big.png is 3150 x 1570. I ran the application successfully when I compiled it for Flash Player 10.

Here's a reference http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#BitmapData%28%29

In AIR 1.5 and Flash Player 10, the maximum size for a BitmapData
object is 8,191 pixels in width or height, and the total number of
pixels cannot exceed 16,777,215 pixels. (So, if a BitmapData object is
8,191 pixels wide, it can only be 2,048 pixels high.) In Flash Player
9 and earlier and AIR 1.1 and earlier, the limitation is 2,880 pixels
in height and 2,880 pixels in width. If you specify a width or height
value that is greater than 2880, a new instance is not created.

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