2011-11-03 60 views
0

我一直在爲自定義Flex預加載程序進行挖掘,它們似乎都依賴於相同的模板:
SWC是使用Flash CS5創建的,然後由Flash Builder使用「preloader」應用程序屬性使用。

我沒有自己的Flash CS,並且覺得Flash Builder應該能夠做到這一點。
我創建了一個圖書館項目在Flash Builder與下面光禿禿的骨頭代碼:
沒有Flash CS的Flex預加載程序

package loader 
{ 
    import flash.display.DisplayObject; 
    import flash.events.Event; 
    import flash.utils.getTimer; 

    import mx.events.RSLEvent; 
    import mx.preloaders.DownloadProgressBar; 
    import mx.preloaders.SparkDownloadProgressBar;  

    public class Preloader extends SparkDownloadProgressBar 
    { 

     [Embed(source="loaderlogo.png")] public var logoClass:Class; 

     private var _displayStartCount:uint = 0; 
     private var _initProgressCount:uint = 0; 
     private var _downloadComplete:Boolean = false; 
     private var _showingDisplay:Boolean = false; 
     private var _startTime:int; 
     // private var preloaderDisplay:PreloaderDisplay; 
     private var rslBaseText:String = "loading: "; 


     public function Preloader() 
     { 
      super(); 
     } 

     /** 
     * Event listener for the <code>FlexEvent.INIT_COMPLETE</code> event. 
     * NOTE: This event can be commented out to stop preloader from completing during testing 
     */ 
     override protected function initCompleteHandler(event:Event):void 
     { 
      dispatchEvent(new Event(Event.COMPLETE)); 
     } 

     /** 
     * Creates the subcomponents of the display. 
     */ 
     override protected function createChildren():void 
     {  
      var img:DisplayObject = new logoClass(); 
      img.x = Math.round((stageWidth - img.width)/2); 
      img.y = Math.round((stageHeight - img.height)/2); 
      addChild(img); 

      var dpb:DownloadProgressBar = new DownloadProgressBar(); 
      dpb.x = img.x + 100; 
      dpb.y = img.x + 100; 
      dpb.width = 170; 
      dpb.height = 20; 
      addChild(dpb); 
     } 

     /** 
     * Event listener for the <code>RSLEvent.RSL_PROGRESS</code> event. 
     **/ 
     override protected function rslProgressHandler(evt:RSLEvent):void { 
      if (evt.rslIndex && evt.rslTotal) { 
       //create text to track the RSLs being loaded 
       rslBaseText = "loading RSL " + evt.rslIndex + " of " + evt.rslTotal + ": "; 
      } 
     } 

     /** 
     * indicate download progress. 
     */ 
     override protected function setDownloadProgress(completed:Number, total:Number):void { 

     } 

     /** 
     * Updates the inner portion of the download progress bar to 
     * indicate initialization progress. 
     */ 
     override protected function setInitProgress(completed:Number, total:Number):void { 
     } 


     /** 
     * Event listener for the <code>FlexEvent.INIT_PROGRESS</code> event. 
     * This implementation updates the progress bar 
     * each time the event is dispatched. 
     */ 
     override protected function initProgressHandler(event:Event):void { 
      var elapsedTime:int = getTimer() - _startTime; 
      _initProgressCount++; 

      if (!_showingDisplay && showDisplayForInit(elapsedTime, _initProgressCount)) { 
       _displayStartCount = _initProgressCount; 
       show(); 
       // If we are showing the progress for the first time here, we need to call setDownloadProgress() once to set the progress bar background. 
       setDownloadProgress(100, 100); 
      } 

      if (_showingDisplay) { 
       // if show() did not actually show because of SWFObject bug then we may need to set the download bar background here 
       if (!_downloadComplete) { 
        setDownloadProgress(100, 100); 
       } 
       setInitProgress(_initProgressCount, initProgressTotal); 
      } 
     } 

     private function show():void 
     { 
      // swfobject reports 0 sometimes at startup 
      // if we get zero, wait and try on next attempt 
      if (stageWidth == 0 && stageHeight == 0) 
      { 
       try 
       { 
        stageWidth = stage.stageWidth; 
        stageHeight = stage.stageHeight 
       } 
       catch (e:Error) 
       { 
        stageWidth = loaderInfo.width; 
        stageHeight = loaderInfo.height; 
       } 
       if (stageWidth == 0 && stageHeight == 0) 
        return; 
      } 

      _showingDisplay = true; 
      createChildren(); 
     } 

    } 
} 

的簡稱,它的加載標誌和一個進度條
它顯示一個預載,但真正晚在加載過程。就好像它在Flex之後加載一樣。
我是否需要在CS5中編譯完全避免使用MX/Spark?

+0

在Flex應用程序,而Flex框架正在初始化中示出了預加載。我不相信Flex Framework的依賴關係的預加載器會像你期望的那樣工作。但是,如果您似乎可行,則可以從Flash Builder ActionScript only項目構建SWF並將其用作預加載器。 – JeffryHouser

+0

這是在啓動過程中出現的東西。所以我有點設法在沒有Flex依賴的情況下進行編譯。加載就好像沒有先加載一樣感覺很長。 – MonoThreaded

+0

除了建議您確保創建僅ActionScript的項目並以這種方式創建預加載器(以確保您沒有Flex依賴關係)之外,我沒有其他評論。我相信任何Flex項目,根據定義,都將具有Flex依賴關係。 – JeffryHouser

回答

1
  1. 您不應該在預加載器中使用任何組件。嘗試刪除你的進口(Ctrl + Shift + O)

    import mx.controls.Image; import spark.components.Label;

  2. 如果需要,可以使用TextField和Loader。我不確定DownloadProgressBar組件。

另外,不要在preloader中使用create children。這裏是一個工作樣品:

package { 
import flash.display.MovieClip; 
import flash.display.Sprite; 
import flash.events.Event; 
import flash.events.ProgressEvent; 
import flash.text.TextField; 
import flash.text.TextFormat; 

import mx.core.mx_internal; 
import mx.preloaders.SparkDownloadProgressBar; 

use namespace mx_internal; 

public class Preloader extends SparkDownloadProgressBar { 

    private var preloaderLogo : MovieClip; 
    private var loadingText : TextField; 
    private var loadingProgress : TextField; 

    private var _initProgressCount : uint = 0; 

    private var textFormat : TextFormat = new TextFormat("Verdana", 16, 0x666666, true); 

    public function Preloader() { 
     super(); 

     textFormat.align = "center"; 
    } 


    override public function set preloader(value : Sprite) : void { 
     super.preloader = value; 

     if (!preloaderLogo) { 
      preloaderLogo = new Assets.PRELOADER_LOGO; // kakaranet logo 

      var startX : Number = Math.round((stageWidth - preloaderLogo.width)/2); 
      var startY : Number = Math.round(stageHeight/2 - preloaderLogo.height) - 100; 

      preloaderLogo.x = startX; 
      preloaderLogo.y = startY; 

      loadingText = new TextField(); 
      loadingProgress = new TextField(); 

      loadingText.width = stageWidth;//to allow center align 
      loadingProgress.width = stageWidth;     


      loadingText.text = "Loading..."; 
      loadingText.y = preloaderLogo.y + preloaderLogo.height + 20; 


      loadingProgress.text = "0%"; 
      loadingProgress.y = loadingText.y + loadingText.textHeight + 10; 

      addChild(preloaderLogo); 
      addChild(loadingText); 
      addChild(loadingProgress); 

      loadingText.setTextFormat(textFormat); 
      loadingProgress.setTextFormat(textFormat); 
     } 
    } 


    override protected function progressHandler(event : ProgressEvent) : void { 
     super.progressHandler(event); 
     if (loadingProgress) { 
      loadingProgress.text = Math.floor(event.bytesLoaded/event.bytesTotal * 100) + "%"; 
      loadingProgress.setTextFormat(textFormat); 
     } 

    } 

    override protected function completeHandler(event : Event) : void { 
     loadingText.text = "Ready!"; 
     loadingText.setTextFormat(textFormat); 
     preloaderLogo.stop(); 
    }   


    override protected function initProgressHandler(event : Event) : void { 
     super.initProgressHandler(event); 
     //similar to super 
     _initProgressCount++; 
     if (loadingProgress) { 
      loadingProgress.text = "100%/" + Math.floor(_initProgressCount/initProgressTotal * 100) + "%"; 
      loadingProgress.setTextFormat(textFormat); 
     } 
    } 
} 

}

+0

這些進口是殘留的,我固定了樣品。 DownloadProgressBar在框架之前加載,所以它應該是好的。感謝您的方便的快捷方式:) – MonoThreaded

+0

我更新了答案樣本和建議不要使用createChildren –