Joh*_*ton 5 urlloader actionscript-3 bitmapdata
我正在尝试加载.SWF文件旁边的图像文件.像这样的东西:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, function(e:Event):void {
trace(typeof(loader.data));
graphic = spritemap = new Spritemap(loader.data, 32, 32);
...
}
Run Code Online (Sandbox Code Playgroud)
但这是我得到的输出:
object
[Fault] exception, information=Error: Invalid source image.
Run Code Online (Sandbox Code Playgroud)
事情是loader.data有图像的字节但不是BitmapData实例,这就是Spritemap所期望的.
如何转换为BitmapData?
谢谢
Mar*_*rty 17
// define image url
var url:URLRequest = new URLRequest("http://sstatic.net/ads/img/careers2-ad-header-so.png");
// create Loader and load url
var img:Loader = new Loader();
img.load(url);
// listener for image load complete
img.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
// attaches the image when load is complete
function loaded(e:Event):void
{
var bitmap:Bitmap = e.target.content;
doStuffWithBitmapData(bitmap.bitmapData);
addChild(bitmap);
// remove listener
e.target.removeEventListener(Event.COMPLETE, loaded);
}
/**
* Handle loaded BitmapData
* @param bmd The loaded BitmapData
*/
function doStuffWithBitmapData(bmd:BitmapData):void
{
trace(bmd);
// your code
}
Run Code Online (Sandbox Code Playgroud)
基本上;
你应该使用Loader,而不是URLLoader.您可以访问BitmapData加载Bitmap的bitmap.bitmapData.
| 归档时间: |
|
| 查看次数: |
17445 次 |
| 最近记录: |