相信有使用過Flash載入外圖檔的朋友們,應該都有過將子的經驗,當載入Gif動畫時,則會發現到,畫面只會停留在Gif動畫的第一格,並不會作播放,真是令人心碎呀!最近便向奶綠老師請益,終於完成了這項不可能的任務,各位也可看到教學網的右上角,第一則就是Gif動態Banner,接下來就為各位來分享這一個不可能的白日夢。
gifPlay.mxml
<?xml version=“1.0” encoding=“utf-8”?>
mx:Script
import flash.net.URLRequest;
import org.gif.player.GIFPlayer;
import org.gif.events.FileTypeEvent;
import org.gif.events.GIFPlayerEvent;
import org.gif.events.FrameEvent;
import org.gif.events.TimeoutEvent;
private var GifPlayer:GIFPlayer = new GIFPlayer();
private var totalFrame:Number;
private function init():void{
var request:URLRequest = new URLRequest(“mwt.gif”);
GifPlayer.load(request);
img.addChild(GifPlayer);
GifPlayer.addEventListener(GIFPlayerEvent.COMPLETE, onCompleteGIF);
GifPlayer.addEventListener(FrameEvent.FRAME_RENDERED, onFrameRendered);
GifPlayer.addEventListener(TimeoutEvent.TIME_OUT, onTimeOut);
}
private function onCompleteGIF(event:GIFPlayerEvent):void{
//取得Gif中的影格數
totalFrame = GifPlayer.totalFrames;
totalframe.text = “TotalFrame:”+String(totalFrame);
}
private function onFrameRendered(event:FrameEvent):void{
//目前正播放的影格
currentframe.text = “CurrentFrame:”+String(GifPlayer.currentFrame);
}
private function onTimeOut(event:TimeoutEvent):void{
trace (“gif error!”);
}
private function playGif():void{
GifPlayer.play();
}
private function stopGif():void{
GifPlayer.stop();
}
範例預覽:
[範例下載]