Actionscript 嵌入一个数组 [英] Actionscript embed an array

查看:24
本文介绍了Actionscript 嵌入一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在编译时嵌入一些图像,所以我最终只有一个 swf.它们需要在数组内,因为我需要以编程方式修改它们,它们可以是 100 幅图像.不能使用 flex,因为我想将整个函数保留在 actionscript 中(又名使文件更小)

我找到了如何在 flex 中做到这一点:

<预><代码>...<mx:Array id="测试"><mx:Image id="image0" source="@Embed(source='../../../lib/Images033,jpg')"/><mx:Image id="image1" source="@Embed(source='../../../lib/Images034,jpg')"/><mx:Image id="image2" source="@Embed(source='../../../lib/Images035,jpg')"/><mx:Image id="image3" source="@Embed(source='../../../lib/Images036,jpg')"/></mx:Array>...addChild(test[1] as something);...

那么有没有人知道如何做上面的,但只是在 Actionscript 中?

非常感谢.

解决方案

好吧,不管怎样,您将需要为每个要嵌入的事物添加一个 Embed 语句,因此您真的无法解决这个问题.但是,如果您更喜欢在脚本中处理所有内容,则可以执行以下操作(这是一个 AIR 应用程序,但 WindowedApplication 标记之间的所有内容都应在普通的 Flex 应用程序中工作):

</mx:脚本></mx:WindowedApplication>

所以基本上你在做什么,在确保你的图像都在编译时嵌入并根据一些数字方案命名(在这种情况下,只是附加一个索引)之后,用类引用填充你的数组,然后在组件生命周期的 createChildren() 阶段实例化它们并将其添加到显示列表中.

这里有一些深奥的东西,所以如果你不是很明白一切,请随时回复评论,我会密切关注.但这是经过测试的代码,考虑到您迄今为止对需求的解释方式,您应该可以很好地工作.

I want to embed some images at compilation time so I end up with just a single swf. They need to be inside of an array as I need to modify them programatically and they can be 100s of images. Cant use flex as I want to keep the whole function in actionscript (aka make files smaller)

I found how to do it in flex:

...
<mx:Array id="test">
   <mx:Image id="image0" source="@Embed(source='../../../lib/Images033,jpg')" />
   <mx:Image id="image1" source="@Embed(source='../../../lib/Images034,jpg')" /> 
   <mx:Image id="image2" source="@Embed(source='../../../lib/Images035,jpg')" />
   <mx:Image id="image3" source="@Embed(source='../../../lib/Images036,jpg')" />
</mx:Array>
...
addChild(test[1] as something);
...

So does anyone know how to do the above but just in Actionscript?

Many thanks.

解决方案

Well, one way or another, you're going to need an Embed statement for every thing you want to embed, so you won't really be able to get around that. But if you'd prefer to handle everything in script, you could do something like this (it's an AIR application, but everything between the WindowedApplication tags should work within a plain ol' Flex app):

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="onInitialize()">

    <mx:Script>
        <![CDATA[

            import mx.controls.Image;

            private var images:Array;
            private const IMAGE_COUNT:uint = 5;

            [Embed(source='Image0.png')]
            private var Image0:Class;

            [Embed(source='Image1.png')]
            private var Image1:Class;

            [Embed(source='Image2.png')]
            private var Image2:Class;

            [Embed(source='Image3.png')]
            private var Image3:Class;

            [Embed(source='Image4.png')]
            private var Image4:Class;

            private function onInitialize():void
            {
                images = new Array(IMAGE_COUNT);

                // Populate your array with Class references to embedded imagery
                for (var i:int = 0; i < IMAGE_COUNT; i++)
                {
                    images[i] = this["Image" + i];
                }
            }

            override protected function createChildren():void
            {
                super.createChildren();

                // Add your children to the display list
                for (var i:int = 0; i < IMAGE_COUNT; i++)
                {
                    var img:Image = new Image();
                    img.source = images[i];

                    addChild(img);
                }
            }

        ]]>
    </mx:Script>

</mx:WindowedApplication>

So essentially what you're doing, after making sure your images are all embedded at compile time and named according to some numeric scheme (in this case, just appended with an index), is filling your array with Class references, then instantiating and adding them to the display list during the createChildren() phase of the component lifecycle.

There's some esoteric stuff going on here, so if you don't quite understand everything, feel free to comment back, and I'll keep an eye on things. But this is tested code and should work pretty well for ya, given how you've explained your requirements so far.

这篇关于Actionscript 嵌入一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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