import flash.display.Sprite; import flash.display.Loader; import flash.net.URLRequest; import flash.events.Event; import flash.net.URLLoader; var swfURL:String="手机报_demo.swf"; var xmlURL:String="newspaper.xml"; var xml:XML; var len:int; var arr_imgName:Array; var arr_imgURL:Array; var arr_selectedPhoto:Array; var page:int;//页索引号 var pageTotal:int; var imgs_perPage:int=4; var oldPhoto:PhotoModule; var photoSprite:Sprite=new Sprite(); addChild(photoSprite); photoSprite.x=85; photoSprite.y=380; //--------------------------------------------------------------------------------------------------- loadSwf(swfURL); loadXML(xmlURL); arr_selectedPhoto=[]; prevBtn.addEventListener(MouseEvent.CLICK,prevBtnClick,false,0,true); nextBtn.addEventListener(MouseEvent.CLICK,nextBtnClick,false,0,true); function loadSwf(url:String):void { var swfLoader:Loader=new Loader(); //addChild(swfLoader); swfLoader.load(new URLRequest(url)); swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfLoaded,false,0,true); } function swfLoaded(e:Event):void { var swf:Loader=e.currentTarget.loader; swf.x=30; swf.y=70; } //--------------------------------------------------------------------------------------------------- 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.BusinessInfo.length();//图片总数 pageTotal=Math.ceil(len/imgs_perPage);//总页数 trace("图片总数len="+len); trace("总页数pageTotal="+pageTotal); arr_imgName=[]; arr_imgURL=[]; for (var i:int=0; i<len; i++) { var imgName:String=xml.BusinessInfo[i].Name; var imgURL:String=xml.BusinessInfo[i].Image; arr_imgName[i]=imgName; arr_imgURL[i]=imgURL; } loadPhotoModule(page); } //--------------------------------------------------------------------------------------------------- function prevBtnClick(e:MouseEvent):void { page--; if (page<0) { page=0; return; } trace("(prevBtn)当前页索引号page="+page); removePhoto(); loadPhotoModule(page); } function nextBtnClick(e:MouseEvent):void { page++; if (page==pageTotal) { page=pageTotal-1; return; } trace("(nextBtn)当前页索引号page="+page); removePhoto(); loadPhotoModule(page); } //--------------------------------------------------------------------------------------------------- function loadPhotoModule(page:int):void { var startIndex:int=page*imgs_perPage; var endIndex:int; if (len%imgs_perPage!=0&&page==pageTotal-1) { endIndex=len; } else { endIndex=startIndex+imgs_perPage; } for (var i:int=startIndex; i<endIndex; i++) { var _name:String=arr_imgName[i]; var url:String=arr_imgURL[i]; var photoModule:PhotoModule=new PhotoModule(_name,url); photoModule.name=String(i); photoModule.buttonMode=true; photoSprite.addChild(photoModule); photoModule.x=(photoModule.width+15)*(i-startIndex); if (arr_selectedPhoto.indexOf(photoModule.name)>=0) { trace("该页包含之前已选中的图片"); photoModule.Selected=true; photoModule.judgeSelected(); oldPhoto=photoModule; } photoModule.addEventListener(PhotoModule.CLICKED,photoModuleClicked,false,0,true); } trace("(loadPhotoModule)已选中图片数组arr_selectedPhoto=["+arr_selectedPhoto+"]"); trace("(loadPhotoModule)已选中图片数组arr_selectedPhoto的长度="+arr_selectedPhoto.length); } //--------------------------------------------------------------------------------------------------- function removePhoto():void { var num:int=photoSprite.numChildren; for (var i:int; i<num; i++) { photoSprite.removeChildAt(0); } } //--------------------------------------------------------------------------------------------------- function photoModuleClicked(e:Event):void { var photo:PhotoModule=e.currentTarget as PhotoModule; if (photo.Selected==true) { if (oldPhoto&&photo!=oldPhoto) { oldPhoto.Selected=false; oldPhoto.judgeSelected(); arr_selectedPhoto.splice(0,1); } oldPhoto=photo; arr_selectedPhoto.push(photo.name); } else { oldPhoto=undefined; arr_selectedPhoto.splice(0,1); } trace("(photoModuleClicked)已选中图片数组arr_selectedPhoto=["+arr_selectedPhoto+"]"); } package { import flash.display.MovieClip; import flash.display.Loader; import flash.net.URLRequest; import flash.events.Event; import flash.events.MouseEvent; public class PhotoModule extends MovieClip { private var limitedWidth:Number=95; private var limitedHeight:Number=65; public var Selected:Boolean; public static const CLICKED:String="clicked"; private var img:Loader; public function PhotoModule(_name:String,url:String):void { tf_name.text=_name; tf_name.selectable=false; bg.y=tf_name.y+tf_name.height+2; gear.y=bg.y+bg.height+3; loadImg(url); addEventListener(MouseEvent.CLICK,clickHandler,false,0,true); } //--------------------------------------------------------------------------------------------------- private function loadImg(url:String):void { var imgLoader:Loader=new Loader ; addChild(imgLoader); setChildIndex(imgLoader,getChildIndex(bg)+1); imgLoader.load(new URLRequest(url)); imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imgLoaded,false,0,true); } private function imgLoaded(e:Event):void { img=e.currentTarget.loader; //img.width=94; //img.height=64; setImgSize(); img.x=bg.x+(bg.width-img.width)/2; img.y=bg.y+(bg.height-img.height)/2; } private function setImgSize():void { if (img.width>limitedWidth) { img.height=limitedWidth/img.width*img.height; img.width=limitedWidth; } if (img.height>limitedHeight) { img.width=limitedHeight/img.height*img.width; img.height=limitedHeight; } } //--------------------------------------------------------------------------------------------------- private function clickHandler(e:MouseEvent):void { if (bg.currentFrame==1) { Selected=true; } else { Selected=false; } judgeSelected(); } public function judgeSelected():void { if (Selected==true) { bg.gotoAndPlay(2); gear.gotoAndPlay(2); } else { bg.gotoAndPlay(1); gear.gotoAndPlay(1); } dispatchEvent(new Event(CLICKED)); } } }