使用Embed标签,我们不仅可以在swf文件中嵌入swf或jpg等,还可以嵌入不支持的文件格式,如:
Embed[(source="mzip.zip", mimeType="application/octet-stream")]
public static MyZip:Class;
当我实例化这样一个类时,我会得到什么类型的对象?是否可以从这个类中获取ByteArray?
当我这样做时:
package {
import flash.utils.ByteArray;
public final class Resource {
[Embed(source="p2.zip", mimeType="application/octet-stream")]
public static const MyZip:Class;
public static function getByteArrayFromZip():ByteArray {
var zip:ByteArray = new MyZip as ByteArray;
return zip;
}
}
public final class Resource {
[Embed(source="p2.zip", mimeType="application/octet-stream")]
public static const MyZip:Class;
public static function getByteArrayFromZip():ByteArray {
var zip:ByteArray = new MyZip as ByteArray;
return zip;
}
}
和
}
public class Main extends Sprite {
private var ship:Bitmap;
private var lib:FZipLibrary;
public function Main() {
lib = new FZipLibrary();
lib.formatAsBitmapData(".jpg");
lib.addEventListener(Event.COMPLETE,onCompleteHandler);
var fzip:FZip = new FZip();
fzip.loadBytes(Resource.getByteArrayFromZip());
lib.addZip(fzip);
}
private function onCompleteHandler(evt:Event) {
var image:BitmapData = lib.getBitmapData("assets/graphics/scratchers/myimg.jpg");
trace("Size: " + image.width + "x" + image.height);
var sa0img0:Bitmap = new Bitmap(image);
addChild(sa0img0);
}
}
我收到以下错误:
public function Main() {
lib = new FZipLibrary();
lib.formatAsBitmapData(".jpg");
lib.addEventListener(Event.COMPLETE,onCompleteHandler);
var fzip:FZip = new FZip();
fzip.loadBytes(Resource.getByteArrayFromZip());
lib.addZip(fzip);
}
private function onCompleteHandler(evt:Event) {
var image:BitmapData = lib.getBitmapData("assets/graphics/scratchers/myimg.jpg");
trace("Size: " + image.width + "x" + image.height);
var sa0img0:Bitmap = new Bitmap(image);
addChild(sa0img0);
}
}
答案 0 :(得分:5)
是的,只需实例化MyZip并转换为ByteArray。
Embed[(source="mzip.zip", mimeType="application/octet-stream")]
public static var MyZip:Class;
var zip:ByteArray = new MyZip() as ByteArray;