載入swf自獲舞臺的寬高

AS3的 LoaderInfo 類爲我們加載外部資源提供了更多的可控信息,以前製作SWF播放器的兩大難題終於可以得到解決:
    * 獲得加載SWF的舞臺大小以縮放到適合尺寸顯示
      LoaderInfo 的 width 和 height 屬性便是舞臺大小。
    * 使加載的SWF按自己的幀頻播放
      LoaderInfo 的 frameRate 屬性爲加載SWF的幀頻,可以修改Stage的 frameRate 屬性適應播放。
做了一個簡單的例子,分別加載300×150幀頻5和100×150幀頻20的兩個swf到200×200大小的區域播放,註釋寫的很詳細,就不囉嗦了。
監聽加載事件:
var t_info : LoaderInfo = this.m_loader.contentLoaderInfo;
t_info.addEventListener(Event.COMPLETE, this.onLoadDone);
t_info.addEventListener(IOErrorEvent.IO_ERROR, this.onLoadError);
t_info.addEventListener(ProgressEvent.PROGRESS, this.onLoadProgress);
顯示加載進度: 
private function onLoadProgress(p_e : ProgressEvent) : void
{
    this.m_loading.progress(p_e.bytesLoaded, p_e.bytesTotal);
}
顯示加載的SWF:
// 隱藏loading
this.m_loading.hide();
// loaderInfo
var t_info : LoaderInfo = this.m_loader.contentLoaderInfo;
// 載入的MC
this.m_mc = t_info.content as MovieClip;
// 載入MC的舞臺寬度
var t_stageW : Number = t_info.width;
// 載入MC的舞臺高度
var t_stageH : Number = t_info.height;
// 載入MC的實際寬度
var t_mcW : Number = this.m_mc.width;
// 載入MC的實際高度
var t_mcH : Number = this.m_mc.height;
// 是否縮放MC適應顯示寬度(載入MC舞臺的寬高比是否大於顯示區域寬高比)
var t_scaleWidth : Boolean = t_stageW / t_stageH > SHOW_W / SHOW_H;
// 縮放比率
var t_scaleRate : Number = t_scaleWidth ? SHOW_W / t_stageW : SHOW_H / t_stageH;
// 縮放MC
this.m_mc.scaleX = this.m_mc.scaleY = t_scaleRate;
// 顯示載入MC的顯示範圍
this.m_mc.scrollRect = new Rectangle(0, 0, t_stageW, t_stageH);
// 顯示載入MC
this.addChild(this.m_mc);
// 調整顯示位置
this.m_mc.x = SHOW_X;
this.m_mc.y = SHOW_Y;
if (t_scaleWidth) this.m_mc.y += (SHOW_H - t_stageH * t_scaleRate) / 2;
else this.m_mc.x += (SHOW_W - t_stageW * t_scaleRate) / 2;
// 修改幀頻
this.stage.frameRate = t_info.frameRate;
this.fms.text = String(this.stage.frameRate);
// 設置組件
this.sdr.enabled = this.btn1.enabled = this.btn2.enabled = true;
this.sdr.maximum = this.m_mc.totalFrames;
// 監聽MC事件
this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
另外加了一個 Slider 組件來控制播放,前提是加載的swf必須發佈爲Player9,As3:
this.sdr.addEventListener(SliderEvent.CHANGE, this.onChangeSdr);
this.sdr.addEventListener(SliderEvent.THUMB_PRESS, this.onPressSdr);
this.sdr.addEventListener(SliderEvent.THUMB_RELEASE, this.onReleaseSdr);
private function onChangeSdr(p_e : SliderEvent) : void
{
    if (this.m_isPressSdr) this.m_mc.gotoAndStop(p_e.value);
}
private function onPressSdr(p_e : SliderEvent) : void
{
    this.m_isPressSdr = true;
    this.m_mc.stop();
}
private function onReleaseSdr(p_e : SliderEvent) : void
{
    this.m_isPressSdr = false;
    this.m_mc.play();
}
[url=http://eidiot.net/lessons/swfplayer/demo/][1b]示例演示[/1b][/url]
[1b] | [/1b]
[url=http://eidiot.net/lessons/swfplayer/code/][1b]源代碼[/1b][/url]
   
本文轉自:http://www.5uflash.com/flashjiaocheng/Flashaschengxu/4470.html
發佈了15 篇原創文章 · 獲贊 0 · 訪問量 1624
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章