此方法適用於用as 1.0或者as2.0以及as3.0編譯的swf,由於as1.0和as2.0編譯的swf是AVM1Movie類型,所以須要經過類ForcibleLoader.as將其轉換爲version 9以上的swf,注意,若是加載的swf是3.0代碼編譯的,且此swf用文檔類編譯,則文檔類必須繼承MovieClip,接下來看代碼:學習
首先寫一個加載swf的類SwfPlayer.as:動畫
1 package com.views 2 { 3 import com.controls.utils.ForcibleLoader; 4 5 import flash.display.AVM1Movie; 6 import flash.display.Loader; 7 import flash.display.MovieClip; 8 import flash.display.Shape; 9 import flash.events.Event; 10 import flash.net.URLRequest; 11 12 /** 13 * ... 14 * @author Frost.Yen 15 */ 16 public class SwfPlayer extends MovieClip 17 { 18 private var _loader:Loader;; 19 private var _urlR:URLRequest; 20 private var _url:String; 21 private var _container:MovieClip; 22 private var _mask:Shape; 23 private var _forcibleLoader:ForcibleLoader; 24 private var _stageW:Number;//swf實際的舞臺寬度 25 private var _stageH:Number;//swf實際的舞臺高度 26 public function SwfPlayer() 27 { 28 _mask = new Shape(); 29 _mask.graphics.beginFill(0); 30 _mask.graphics.drawRect(0, 0, 10, 10); 31 _mask.graphics.endFill(); 32 this.mask = _mask; 33 } 34 /** 35 * 加載swf 36 * @param url swf路徑 37 */ 38 public function Load(url:String):void{ 39 this._url=url; 40 if(!_loader){ 41 _loader= new Loader() 42 } 43 _urlR=new URLRequest(_url); 44 _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete); 45 _loader.load(_urlR); 46 } 47 48 private function onComplete(event:Event):void { 49 50 if (_loader.content is AVM1Movie) {//若是是as2.0或者1.0代碼生成的swf 51 trace("_loader.content is AVM1Movie"); 52 _loader.unloadAndStop(); 53 _forcibleLoader = new ForcibleLoader(_loader); 54 _forcibleLoader.load(_urlR); 55 return; 56 } 57 _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete); 58 trace(_loader.contentLoaderInfo,_loader,"_loader"); 59 try 60 { 61 _container = MovieClip(_loader.content); 62 _stageW = _loader.contentLoaderInfo.width; 63 _stageH = _loader.contentLoaderInfo.height; 64 this.addChild(_container); 65 } 66 catch(error:Error) 67 { 68 trace(_loader.width,"_loader.width"); 69 70 _stageW = _loader.width; 71 _stageH = _loader.height; 72 this.addChild(_loader); 73 } 74 75 _mask.width = _stageW; 76 _mask.height = _stageH; 77 78 this.addChild(_mask); 79 this.dispatchEvent(new Event(Event.COMPLETE)); 80 } 81 /** 82 * 播放下一幀 83 */ 84 public function nextPlay():void{ 85 if(_container.currentFrame==_container.totalFrames){ 86 stop(); 87 }else{ 89 _container.nextFrame(); 90 } 91 } 92 /** 93 * 播放上一幀 94 */ 95 public function prevPlay():void{ 96 if(_container.currentFrame==1){ 97 stop(); 98 }else{ 100 _container.prevFrame(); 101 } 102 } 103 /** 104 * 開始播放 105 */ 106 public function startPlay():void 107 { 108 _container.play(); 109 } 110 /** 111 * 暫停播放 112 */ 113 public function pausePlay():void 114 { 115 _container.stop(); 116 } 117 118 /** 119 * 卸載加載的swf 120 */ 121 public function unloadAndStop():void 122 { 123 124 if(_loader){ 125 _loader.unloadAndStop(); 126 _loader = null; 127 128 } 129 if(_container){ 130 _container.parent.removeChild(_container); 131 _container = null; 132 } 133 134 } 135 136 public function get stageW():Number 137 { 138 return _stageW; 139 } 140 141 public function set stageW(value:Number):void 142 { 143 _stageW = value; 144 } 145 146 public function get stageH():Number 147 { 148 return _stageH; 149 } 150 151 public function set stageH(value:Number):void 152 { 153 _stageH = value; 154 } 155 156 } 157 158 }
而後在flash文檔類Main.as中調用,flash文檔舞臺上有兩個控制按鈕stopBtn,playBtn:this
1 package 2 { 3 import com.views.SwfPlayer; 4 import flash.display.Sprite; 5 import flash.events.MouseEvent; 6 /** 7 * ... 8 * @author Frost.Yen 9 */ 10 public class Main extends Sprite 11 { 12 private var _swfPlayer:SwfPlayer; 13 public function Main() 14 { 15 _swfPlayer = new SwfPlayer(); 16 this.addChild(_swfPlayer); 17 _swfPlayer.load("D:/Flash學習/flash動畫做品/1/1.swf"); 18 stopBtn.addEventListener(MouseEvent.CLICK,onStop); 19 playBtn.addEventListener(MouseEvent.CLICK,onPlay); 20 } 21 private function onStop(e:MouseEvent):void 22 { 23 _swfPlayer.pausePlay();//暫停播放 24 } 25 private function onPlay(e:MouseEvent):void 26 { 27 _swfPlayer.startPlay();//開始播放 28 } 29 } 30 }
附:此類ForcibleLoader.as可到http://download.csdn.net/detail/yan_frost/4771007下載url