flash as3: 关于自定义载入图片类。代码如下
----------类文件:img.imgload.as----------packageimg{importflash.net.URLLoader;importflas...
----------类文件:img.imgload.as----------
package img{
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
public class imgload {
private var empMC:MovieClip = new MovieClip();
private var urlLoader:URLLoader = new URLLoader();
private var _loader:Loader = new Loader();
public var imgsource:URLRequest;
public var isMC:MovieClip;
public var loadingMC:MovieClip;
public var iswidth:int;
public var isheight:int;
public var totalbyte:int;
public var loadedbyte:int;
public function imgload() {
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, loadpic);
}
public function loadstart() {
urlLoader.load(imgsource);
}
public function loadpic(evt:Event):void {
_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, LoadComplete);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
_loader.loadBytes(evt.target.data);
}
public function LoadComplete(evt:ProgressEvent):void {
totalbyte = evt.bytesTotal;
loadedbyte = evt.bytesLoaded;
loadingMC.gotoAndStop(Math.floor(totalbyte/loadedbyte));
}
public function onComplete(evt:Event):void {
var _bitmapdata = new BitmapData(_loader.width, _loader.height, true, 0x00000000);
_bitmapdata.draw(_loader);
var _bitmap = new Bitmap(_bitmapdata);
empMC.addChild(_bitmap);
empMC.width = iswidth;
empMC.height = isheight;
isMC.addChild(empMC);
}
}
}
-----------------------------------------------------------
-----------fla文件:----------------------
import img.imgload;
import flash.display.MovieClip;
import flash.net.URLRequest;
var mc:MovieClip = new MovieClip();
var uu = new imgload();
uu.isMC = mc;
uu.isheight = 150;
uu.iswidth = 150;
uu.imgsource = new URLRequest("1.jpg");
uu.loadstart();
addChild(mc);
---------------------------------------------------------------------------
错误提示是:访问的属性URLLoaderDataFormat未定义。
主要想实现,只需在fla中给出图片地址和指定影片剪辑,类自动可以加载图片到指定的影片剪辑中。望高手给予指点,非常感谢! 展开
package img{
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
public class imgload {
private var empMC:MovieClip = new MovieClip();
private var urlLoader:URLLoader = new URLLoader();
private var _loader:Loader = new Loader();
public var imgsource:URLRequest;
public var isMC:MovieClip;
public var loadingMC:MovieClip;
public var iswidth:int;
public var isheight:int;
public var totalbyte:int;
public var loadedbyte:int;
public function imgload() {
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, loadpic);
}
public function loadstart() {
urlLoader.load(imgsource);
}
public function loadpic(evt:Event):void {
_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, LoadComplete);
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
_loader.loadBytes(evt.target.data);
}
public function LoadComplete(evt:ProgressEvent):void {
totalbyte = evt.bytesTotal;
loadedbyte = evt.bytesLoaded;
loadingMC.gotoAndStop(Math.floor(totalbyte/loadedbyte));
}
public function onComplete(evt:Event):void {
var _bitmapdata = new BitmapData(_loader.width, _loader.height, true, 0x00000000);
_bitmapdata.draw(_loader);
var _bitmap = new Bitmap(_bitmapdata);
empMC.addChild(_bitmap);
empMC.width = iswidth;
empMC.height = isheight;
isMC.addChild(empMC);
}
}
}
-----------------------------------------------------------
-----------fla文件:----------------------
import img.imgload;
import flash.display.MovieClip;
import flash.net.URLRequest;
var mc:MovieClip = new MovieClip();
var uu = new imgload();
uu.isMC = mc;
uu.isheight = 150;
uu.iswidth = 150;
uu.imgsource = new URLRequest("1.jpg");
uu.loadstart();
addChild(mc);
---------------------------------------------------------------------------
错误提示是:访问的属性URLLoaderDataFormat未定义。
主要想实现,只需在fla中给出图片地址和指定影片剪辑,类自动可以加载图片到指定的影片剪辑中。望高手给予指点,非常感谢! 展开
2011-08-12
展开全部
package img{
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
加多一句
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
加多一句
追问
非常感谢,图片可以载进来了,但在输出窗口中有下面提示,不知在网上运行时受不受影响.
TypeError: Error #1009: 无法访问空对象引用的属性或方法。
at img::imgload/LoadComplete()
at flash.display::Loader/_loadBytes()
at flash.display::Loader/loadBytes()
at img::imgload/loadpic()
追答
loadingMC.gotoAndStop(Math.floor(totalbyte/loadedbyte));
改成
//loadingMC.gotoAndStop(Math.floor(totalbyte/loadedbyte));
展开全部
至于为什么访问不到那个属性我不太清楚,估计是你拼写错误,检查下;我想提醒的是,URLLoader主要是用来加载文本、二进制和变量类型的数据,像你加载图片,最好是用Loader;从他们所属的包就可以看出来:flash.net.URLLoader是网络包,Flash.dispaly.Loader是属于display类的嘛。用URLLoader来加载图片虽然不能说不对,但至少不合适,而且像你这样处理肯定不会成功。刚好我今天就写了一个加载图片的,我贴上来你参考下。//***********package{ import flash.display.Sprite; import flash.events.Event; import flash.display.Loader; import flash.net.URLRequest; import flash.display.Bitmap; import flash.display.BitmapData; / * @author dexline */ public class Main extends Sprite { private var loader:Loader = new Loader(); private var adress:URLRequest = new URLRequest(); public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); stage.scaleMode = "noScale" stage.align = "top" //************************************** loader.contentLoaderInfo.addEventListener("complete", onComplete) adress.url = "
" loader.load(adress) } private function onComplete(e:Event):void { loader.contentLoaderInfo.removeEventListener("complete", onComplete) var bm:Bitmap = loader.content as Bitmap; bm.scaleX=bm.scaleY=0.7//图片较大,缩小一点,可省略。 addChild(bm) } }}
" loader.load(adress) } private function onComplete(e:Event):void { loader.contentLoaderInfo.removeEventListener("complete", onComplete) var bm:Bitmap = loader.content as Bitmap; bm.scaleX=bm.scaleY=0.7//图片较大,缩小一点,可省略。 addChild(bm) } }}
追问
谢谢你的回答,你的代码让我学到了新的东西,虽然我已按前面的方法实现了,但还是要谢谢你对我的帮助。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询