Embed about 90 images in the flash
I have about 90 images which I need to embed into action script (version 3). The only way I could figure out is ty开发者_运维问答ping the following code 90 times for each image.
[Embed(source="images/door0091.bmp")]
private var Door1:Class;
var door1:Bitmap = new Door1();
It's really painful to write it so many times. I would like to know if there is any way to do it programatically?
I cannot use the loader method as the images need to be present along with the swf file. I want the swf to be independent once it is compiled. Please Help!!!
Thanks in advance.
If you are on Windows, you could use a batch script to generate the file that contains the embed code (or generate part of it and manually paste it in another file).
Batch is ugly, but for simple tasks it's almost usable. Copy the following code into a new file and name it whatever you like (use .bat as its extension). Put this script in your images folder and run it (double click on it). It should create a file images.as
with the generated actionscript code for each image that ends in .jpg (you could change the script to use other extensions).
@echo off
set out_file=images.as
echo // script generated file > %out_file%
for /f %%a IN ('dir /b *.jpg') do (
echo [Embed^(source="images/%%a"^)] >> %out_file%
echo private var %%~na_class:Class; >> %out_file%
echo private var %%~na:Bitmap = new %%~na_class^(^); >> %out_file%
)
This is quite basic and will not work well if the name of your files contains "weird" characters such as space. But otherwise, it could do the heavy lifting, I think.
There is a way - if you want to use a zip file. You can embed the whole zip file containing all of the images into your application. Then, open the zip file at runtime to retrieve the images you need. You can write your own zip util, or, as shown below, just use the free one from nochump:
When you call the zip.getInput() function, that's when that single zip entry's contents are extracted, so use that call only when you are ready to pull out a specific file.
Here's an entire demo application:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import nochump.util.zip.ZipEntry;
import nochump.util.zip.ZipFile;
[Embed(source="images.zip",mimeType="application/octet-stream")]
private var imagesZip:Class;
protected function init():void
{
var theImages:Object = new imagesZip();
var zip:ZipFile = new ZipFile(theImages as IDataInput);
for each(var entry:ZipEntry in zip.entries)
{
var fileName:String = entry.name.toLowerCase();
if(fileName == "image2.jpg")
{
var loader:Loader = new Loader();
this.rawChildren.addChild(loader);
loader.loadBytes(zip.getInput(entry));
}
}
}
]]>
</mx:Script>
</mx:Application>
I hope that helps you out!
Embed is something done in Compile Time you can't do it programatically.
the only think you can do is use path and generate it in an array. or if you satisfy with embed i agree with Juan Pablo Clifano trick or typically using code generator such as Code Smith etc.
精彩评论