分批载入:载入XML数与图片数不相同(一次载入25个xml图片地址,点击下一页载入5张图片)(存在BUG,未修复)
import flash.display.Loader; import flash.net.URLRequest; import flash.net.URLLoader; import flash.events.Event; import flash.display.Sprite; var xmlURL:String="test.xml"; var xml:XML; var arr_imgURL:Array=[]; var len:int; var _value1:Number; var _value2:Number;//用来判断最后一页是否包含5个图片 var pageNum:int;//总页数 var lastXmlPartIndex:int;//最后一段xml的索引号 var lastPartImgs:int;//最后一段xml中包含的图片数目 var lastPage:int;//最后一段xml中的最后一页索引号 var imgs_perPage:int=5;//每页图片数目 var pages_perXmlPart:int=5;//每段xml中包含页数 var imgs_perXmlPart:int=imgs_perPage*pages_perXmlPart;//每段xml中包含图片数目 var distance:int=10; var page:int; var xmlPartIndex:int; var imgSprite:Sprite=new Sprite(); addChild(imgSprite); //--------------------------------------------------------------------------------------------------- loadXML(xmlURL); function loadXML(url:String):void { var xmlLoader:URLLoader=new URLLoader(); xmlLoader.load(new URLRequest(url)); xmlLoader.addEventListener(Event.COMPLETE,xmlLoaded,false,0,true); } function xmlLoaded(e:Event):void { xml=XML(e.currentTarget.data); len=xml.img.length();//图片总数 _value1=len/imgs_perXmlPart;//xml段数(浮点值) _value2=len/imgs_perPage;//用来判断最后一页是否包含5个图片 pageNum=Math.ceil(_value2);//总页数 lastXmlPartIndex=Math.ceil(_value1);//最后一段xml的索引号 lastPartImgs=len-(lastXmlPartIndex-1)*imgs_perXmlPart;//最后一段xml中包含的图片数目 lastPage=Math.ceil(lastPartImgs/imgs_perPage);//最后一段xml中的最后一页索引号 trace("len="+len); trace("_value1="+_value1); trace("_value2="+_value2); trace("pageNum="+pageNum); trace("lastXmlPartIndex="+lastXmlPartIndex); trace("lastPartImgs="+lastPartImgs); trace("lastPage="+lastPage); setImgURL(xmlPartIndex); loadImg(page); tf_page.text=String("当前页索引:"+page); tf_xmlPartIndex.text=String("当前xml段号:"+xmlPartIndex); prevBtn.addEventListener(MouseEvent.CLICK,prevBtnClick,false,0,true); nextBtn.addEventListener(MouseEvent.CLICK,nextBtnClick,false,0,true); } //--------------------------------------------------------------------------------------------------- function prevBtnClick(e:MouseEvent):void { removeImg(); page--; if (page==-1) { xmlPartIndex--; page=pages_perXmlPart-1; setImgURL(xmlPartIndex); } loadImg(page); tf_page.text=String("当前页数:"+page); tf_xmlPartIndex.text=String("当前xml段号:"+xmlPartIndex); } function nextBtnClick(e:MouseEvent):void { removeImg(); page++; if (page>lastPage) { return; } if (page==pages_perXmlPart) { xmlPartIndex++; page=0; setImgURL(xmlPartIndex); } loadImg(page); tf_page.text=String("当前页数:"+page); tf_xmlPartIndex.text=String("当前xml段号:"+xmlPartIndex); } //--------------------------------------------------------------------------------------------------- function setImgURL(xmlPartIndex:int):void { var startIndex:int; var endIndex:int; if (!(_value1 is int)&&xmlPartIndex==lastXmlPartIndex) { startIndex=imgs_perXmlPart*(lastXmlPartIndex-1); endIndex=len; } else { startIndex=imgs_perXmlPart*xmlPartIndex; endIndex=imgs_perXmlPart*(xmlPartIndex+1); } for (var i:int=startIndex; i<endIndex; i++) { var imgURL:String=xml.img[i].picture.@url; arr_imgURL[i-startIndex]=imgURL; } } //--------------------------------------------------------------------------------------------------- function loadImg(page:int):void { var startIndex:int=page*imgs_perPage; var endIndex:int; if (!(_value2 is int)&&xmlPartIndex==lastXmlPartIndex&&page==lastPage) { endIndex=lastPartImgs; } else { endIndex=(page+1)*imgs_perPage; } for (var i:int=startIndex; i<endIndex; i++) { var url:String=arr_imgURL[i]; trace(url); var imgLoader:Loader=new Loader(); imgLoader.name=String(i-startIndex); imgSprite.addChild(imgLoader); imgLoader.load(new URLRequest(url)); imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imgLoaded,false,0,true); } } function imgLoaded(e:Event):void { var image:Loader=e.currentTarget.loader; var index:int=int(image.name); image.width=100; image.height=100; image.x=20+(image.width+distance)*index; image.y=100; trace("当前已载入的图片:"+(index+page*pages_perXmlPart)); } //--------------------------------------------------------------------------------------------------- function removeImg():void { var num:int=imgSprite.numChildren; for (var i:int=0; i<num-1; i++) { imgSprite.removeChildAt(0); } }