`
qs421qs
  • 浏览: 12159 次
社区版块
存档分类
最新评论

[转] [flash/flex] 第二章 第二节: 嵌入式资源-as3-javaeye技术网站

 
阅读更多

[转] [flash/flex]  第二章 第二节: 嵌入式资源-as3-javaeye技术网站
2011年03月01日
  http://bbs.9ria.com/viewthread.php?tid=76876&extra =page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter %3D2592000
  嵌入式资源 
  另一个常用功能就是嵌入外部资源(XML或者Pixel Bender Kernel 滤镜)时会用到.我们经常会碰到的问题就是将那些运行时依赖的资源嵌入到外部swf中.一些服务端拒绝swf拥有外部依赖(external dependency),所以假设你开发了一个小型的应用或游戏并且在几分钟后你需要快速的删除所有外部依赖(external dependency),你可以使用Embed标签来实现这一功能. 
  在下面的代码中,我们使用Embed标签来嵌入一个外部的 Pixel Bender 滤镜 
  [Embed(source="myFilter.pbj", mimeType="application/octet-stream")] var myShaderKernel:Class; 
  在编译时, Pixel Bender 滤镜将被当做一个ByteArray嵌入,注意mimeType application/octet-stream的用途就是允许嵌入资源被当做ByteArray. 
  更一般的我们也可以将XML作为嵌入资源: 
  import flash.utils.ByteArray; [Embed(source="test.xml", mimeType="application/octet-stream")] var xmlStream:Class; // instanciate the stream as a ByteArray var xmlBytes:ByteArray = new xmlStream(); // read the XML String from the byte stream var xmlString:String = xmlBytes.readUTFBytes( xmlBytes.bytesAvailable ); // instanciate a XML object by passing the content var myXML:XML = new XML(xmlString); /* outputs :      */ trace ( myXML ); // outputs : 3 trace ( myXML.item.length() ); 
  任何东西都可以通过这种方法嵌入,我们通过bytearray的API来操作这里嵌入的原始二进制数据 .但需要强调的是上一段代码有一点冗余,所以我们可以通过使用特定的mimeType,来强制的要求转码器将其映射为特定的类型.在下列的代码中,你会看到同样的嵌入使用mimeType后代码变得极其简单. 
  import flash.utils.ByteArray; [Embed(source="test.xml", mimeType="text/xml")] var xmlStream:Class; // instanciate a XML object by passing the content var myXML:XML = new XML (xmlStream.data); /* outputs :      */ trace ( myXML ); // outputs : 3 trace ( myXML.item.length() ); 
  很便捷吧?接下来让我们来了解一下怎么样在运行时注入和重建bytes的内容.Adobe Flash Player 提供了一个非常强大的API来处理这种情景. 
  注入bytes 
  一般来说注入的对象可以是图像,字体,甚至是swf.Flash player 中没有SWF类型,但是你猜怎么着?Loader对象的API中有一个loadBytes. 
  这就是允许我们将swf当做一个ByteArray注入到Loader对象中并可以运行它: 
  import flash.utils.ByteArray; import flash.display.Loader; [Embed(source="test-loading.swf", mimeType="application/octet-stream")] var swfStream:Class; // instanciate the stream as a ByteArray var swfBytes:ByteArray = new swfStream(); // instanciate a Loader var myLoader:Loader = new Loader(); // inject the embedded stream inside the Loader // the SWF is executed automatically myLoader.loadBytes(swfBytes); 
  为了显示它,我们只需要将Loader添加到显示列表即可: 
  import flash.utils.ByteArray; import flash.display.Loader; [Embed(source="test-loading.swf", mimeType="application/octet-stream")] var swfStream:Class; // instanciate the stream as a ByteArray var swfBytes:ByteArray = new swfStream(); // instanciate a Loader var myLoader:Loader = new Loader(); // show the Loader object addChild ( myLoader ); // inject the embedded stream inside the Loader // the SWF is executed automatically What can you do with bytes ?
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics