xml_code:
<?xml version="1.0" encoding="utf-8" ?> <root> <img imgUrl='/image/01.jpg' imgName="K歌" imgType ="gif" imgLink="http://baike.baidu.com/view/10114.htm"/> <img imgUrl='/image/02.jpg' imgName="拜拜" imgType ="gif" imgLink="http://blog.sina.com.cn/cangyue"/> <img imgUrl='/image/03.jpg' imgName="吃西瓜" imgType ="gif" imgLink="http://tieba.baidu.com/f?kw=%B2%D7%D4%C2&fr=ala0"/> <img imgUrl='/image/04.jpg' imgName="放假咯" imgType ="gif" imgLink="http://book.sina.com.cn/people/cangyue/"/> <img imgUrl='/image/05.jpg' imgName="加油打气" imgType ="gif" imgLink="http://baike.baidu.com/view/1584711.htm"/> <img imgUrl='/image/06.jpg' imgName="接力赛" imgType ="gif" imgLink="http://baike.baidu.com/view/964114.htm"/> <img imgUrl='/image/07.jpg' imgName="看球赛" imgType ="gif" imgLink="http://baike.baidu.com/view/884808.htm"/> </root>
as_code:
1 package 2 { 3 import flash.display.MovieClip; 4 import flash.display.StageAlign; 5 import flash.display.StageScaleMode; 6 import flash.display.StageDisplayState; 7 import flash.events.Event; 8 import flash.events.MouseEvent; 9 import flash.events.TimerEvent; 10 import flash.utils.Timer; 11 import flash.display.Loader; 12 import flash.net.URLRequest; 13 import flash.net.URLLoader; 14 import flash.net.navigateToURL; 15 import gs3.TweenLite; 16 import flash.display.Sprite; 17 18 19 public class Main extends MovieClip 20 { 21 private var url:String = "./img.xml"; 22 private var myXml:XML; 23 private var len:int = 0; 24 private var imgUrl:Array = []; 25 private var imgName:Array = []; 26 private var imgLink:Array = []; 27 private var bt1Arr:Array = [];//装小按钮 28 private var bt2Arr:Array = [];//装移动按钮 29 private var btMc:MovieClip;//放按钮的剪辑 30 private var imgMc:MovieClip;//放图片的剪辑 31 private var imgWidth:Number = 0;//图片的宽度 32 private var imgHeight:Number = 0;//图片的高度 33 private var firstLoad:Boolean = true;//是否是第一次 34 private var maskMc:Sprite;//遮罩 35 private var num:int = 0;// 当前的图片移动的第几张图片 36 private var gapNum:int = 10;//小按钮之间的间隔 37 private var myTimer:Timer; 38 private var timeNum:int = 5;//每4秒,切换一次图片 39 private var domain:String=".";//"http://www.vini123.com/demo/img1" 40 41 42 public function Main():void 43 { 44 if (stage) 45 { 46 Init(null); 47 } 48 else 49 { 50 addEventListener(Event.ADDED_TO_STAGE,Init); 51 } 52 53 } 54 55 private function Init(e:Event=null) 56 { 57 var leftFun=new LeftFun(this); 58 stage.scaleMode = StageScaleMode.NO_SCALE; 59 stage.align = StageAlign.TOP_LEFT; 60 imgMc=new MovieClip(); 61 btMc=new MovieClip(); 62 addChild(imgMc); 63 addChild(btMc); 64 myXml=new XML(); 65 loadXml(url); 66 } 67 private function loadXml(url:String) 68 { 69 var myLoader:URLLoader=new URLLoader(); 70 myLoader.load(new URLRequest(url)); 71 myLoader.addEventListener(Event.COMPLETE,xmlCompleteHandler); 72 } 73 private function xmlCompleteHandler(e:Event) 74 { 75 e.target.removeEventListener(Event.COMPLETE,xmlCompleteHandler); 76 myXml = XML(e.target.data); 77 len = myXml.img.length(); 78 for (var i:int=0; i<len; i++) 79 { 80 imgUrl.push(myXml.img[i].@imgUrl); 81 imgName.push(myXml.img[i].@imgName); 82 imgLink.push(myXml.img[i].@imgLink); 83 } 84 85 for (i=0; i<len; i++) 86 { 87 loadImg(domain+imgUrl[i],i); 88 } 89 } 90 91 private function loadImg(url:String,num) 92 { 93 var myLoader:Loader=new Loader(); 94 myLoader.load(new URLRequest(url)); 95 myLoader.name = num; 96 myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imgCompleteHandler); 97 } 98 99 private function imgCompleteHandler(e:Event) 100 { 101 e.target.removeEventListener(Event.COMPLETE,imgCompleteHandler); 102 if (firstLoad) 103 { 104 firstLoad = false; 105 imgWidth = e.target.content.width; 106 imgHeight = e.target.content.height; 107 maskMc=new Sprite(); 108 maskMc.graphics.beginFill(0xff0000); 109 maskMc.graphics.drawRect(0,0,imgWidth,imgHeight); 110 maskMc.graphics.endFill(); 111 addChild(maskMc); 112 imgMc.mask = maskMc; 113 imgMc.buttonMode = true; 114 imgMc.addEventListener(MouseEvent.ROLL_OVER,imgOverHandler); 115 imgMc.addEventListener(MouseEvent.ROLL_OUT,imgOutHandler); 116 imgMc.addEventListener(MouseEvent.CLICK,imgClickHandler); 117 layBtn(); 118 } 119 e.target.content.x = imgWidth * int(e.target.loader.name); 120 e.target.content.y = 0; 121 imgMc.addChild(e.target.content); 122 if (int(e.target.loader.name)==(len-1)) 123 { 124 timeControl(); 125 } 126 } 127 private function layBtn() 128 { 129 len = imgUrl.length; 130 bt1Arr = []; 131 bt2Arr = []; 132 for (var i:int=0; i<len; i++) 133 { 134 var bt1=new Mc1(); 135 bt1.name = i; 136 bt1.buttonMode = true; 137 bt1.x=(imgWidth-(bt1.width+gapNum)*len)/2 + (bt1.width+gapNum)*i+gapNum/2; 138 bt1.y = imgHeight + 5; 139 bt1.addEventListener(MouseEvent.ROLL_OVER,overHandler); 140 btMc.addChild(bt1); 141 bt1Arr.push(bt1); 142 } 143 144 for (i=0; i<2; i++) 145 { 146 var bt2=new Mc2(); 147 bt2.name = i; 148 if (i==0) 149 { 150 bt2.x = 15 + bt2.width; 151 bt2.rotation = 180; 152 bt2.y = imgHeight / 2 + bt2.height / 2; 153 } 154 else 155 { 156 bt2.x = imgWidth - 15 - bt2.width; 157 bt2.y = imgHeight / 2 - bt2.height / 2; 158 } 159 bt2.buttonMode = true; 160 bt2.addEventListener(MouseEvent.ROLL_OVER,bt2OverHandler); 161 bt2.addEventListener(MouseEvent.ROLL_OUT,bt2OutHandler); 162 bt2.addEventListener(MouseEvent.CLICK,bt2ClickHandler); 163 btMc.addChild(bt2); 164 bt2Arr.push(bt2); 165 } 166 trace(imgWidth,imgHeight,bt1Arr[0].height); 167 setBt2(false); 168 bt1Arr[0].gotoAndStop(5); 169 } 170 171 private function overHandler(e:MouseEvent) 172 { 173 num = int(e.target.name); 174 moveImg(); 175 //setBt2(true); 176 } 177 178 private function bt2OverHandler(e:MouseEvent) 179 { 180 e.target.gotoAndStop(5); 181 } 182 183 private function bt2OutHandler(e:MouseEvent) 184 { 185 e.target.gotoAndStop(1); 186 } 187 188 private function bt2ClickHandler(e:MouseEvent) 189 { 190 switch (e.target.name) 191 { 192 case "0" : 193 num--; 194 break; 195 case "1" : 196 num++; 197 break; 198 } 199 moveImg(); 200 } 201 202 203 private function moveImg() 204 { 205 if (num>=len) 206 { 207 num = 0; 208 } 209 if (num<0) 210 { 211 num = len - 1; 212 } 213 TweenLite.to(imgMc,0.8,{x:-imgWidth*num}); 214 resetBtn(); 215 } 216 private function imgOverHandler(e:MouseEvent) 217 { 218 setBt2(true); 219 } 220 221 private function imgOutHandler(e:MouseEvent) 222 { 223 setBt2(false); 224 } 225 226 private function imgClickHandler(e:MouseEvent) 227 { 228 navigateToURL(new URLRequest(imgLink[num]),"_blank"); 229 } 230 231 private function setBt2(right:Boolean) 232 { 233 var i:int = 0; 234 235 for (i=0; i<2; i++) 236 { 237 if (bt2Arr[i].hitTestPoint(mouseX,mouseY,false)) 238 { 239 return; 240 } 241 242 } 243 244 if (right) 245 { 246 for (i=0; i<2; i++) 247 { 248 bt2Arr[i].visible = true; 249 } 250 } 251 else 252 { 253 for (i=0; i<2; i++) 254 { 255 bt2Arr[i].visible = false; 256 } 257 } 258 259 } 260 261 private function resetBtn() 262 { 263 trace("abcd"); 264 trace("aa",num); 265 for (var i:int=0; i<len; i++) 266 { 267 if (i!=num) 268 { 269 bt1Arr[i].gotoAndStop(1); 270 } 271 else 272 { 273 bt1Arr[i].gotoAndStop(5); 274 } 275 } 276 } 277 278 /** 279 *时间控制 280 */ 281 private function timeControl() 282 { 283 trace("gg"); 284 myTimer = new Timer(1000,timeNum); 285 myTimer.start(); 286 myTimer.addEventListener(TimerEvent.TIMER_COMPLETE,timerCompleteHandler); 287 } 288 289 private function timerCompleteHandler(e:TimerEvent) 290 { 291 trace("over"); 292 myTimer.stop(); 293 myTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,timerCompleteHandler); 294 myTimer = null; 295 num++; 296 moveImg(); 297 timeControl(); 298 } 299 } 300 }