2009-12-29 91 views
1

我有一個圖像,我正試圖加載,然後重新加載。這是我對圖像的加載代碼:as3:重新加載圖像

public function loadImage(url:String, _w:int, _h:int):void 
    { 
     this._stwidth = _w; 
     this._stheight = _h; 
     this._imageURL = url; 

     if(!_imageURL) 
     { 
      return; 
     } 

     this.alpha = 1.0; //need this because we might have just faded the image out 

     _ldr.alpha = 0.0; 
     _prog.alpha = 1.0; 
     _sqr.alpha = 0.0; 
     _sqr.graphics.clear(); 

     if(_hasLoaded) 
     { 
      try 
      { 
       _ldr.close(); 
       _ldr.unload();     
      } 
      catch(e:Error) 
      { 
       //trace("bmdisplay has loaded once, but there was an error: " + e.message); 
      }   
     } 

     _ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress); 
     _ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); 
     _ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError); 
     _ldr.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); 
     _ldr.contentLoaderInfo.addEventListener(Event.INIT, onOpen); 
     _ldr.load(new URLRequest(_imageURL)); 
    } 

出於某種原因,沒有第2次負載發出錯誤的代碼將不會加載圖像。 有人可以幫我解決這個問題嗎?

我完全失去了爲什麼我的變量_asLoaded會做我的錯。 我有一個onComplete()處理程序,它將該var設置爲true,之後我從未將其設置爲false。

我不知道還有什麼我應該嘗試...

感謝

回答

1

我將宣佈_ldr裏面的功能所以它的死每次啓動這個功能。我也不會使用這個unload()close()方法。它更簡單,如果做這樣的事情(你需要有一個空的movieclip叫做「ldrHelper」):

public function loadImage(url:String, _w:int, _h:int):void 
{ 
    // do your job and die bravely, no need to be global 
    var _ldr:Loader = new Loader(); 
    this._stwidth = _w; 
    this._stheight = _h; 
    this._imageURL = url; 

    if(!_imageURL) 
    { 
     return; 
    } 

    this.alpha = 1.0; //need this because we might have just faded the image out 

    // now you will need to make alpha = 1 on ldrHolder since _ldr is dead after this function 
    ldrHolder.alpha = 0.0; 
    _prog.alpha = 1.0; 
    _sqr.alpha = 0.0; 
    _sqr.graphics.clear(); 


    // remove the old image, doesn't matter whether its empty or not 
    while(ldrHolder.numChildren > 0){ 
     ldrHolder.removeChildAt(0); 
    } 

    //add image 
    ldrHolder.addChild(_ldr); 

    _ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress); 
    _ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); 
    _ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError); 
    _ldr.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); 
    _ldr.contentLoaderInfo.addEventListener(Event.INIT, onOpen); 
    _ldr.load(new URLRequest(_imageURL)); 
} 
+0

我已經想到了這一點,雖然我喜歡你的解決方案,使用removeChild()和addChild()來包裝它。最終,我目前正在運行的(並且不會產生任何錯誤的)答案是簡單地重新加載一個新的映像,_不再循環加載器,_not_ try/catch,並且在完成此類之上的其他類時爲其分派事件在顯示列表鏈中。 – jml 2010-01-18 10:46:55

2

有時回我寫了一個輔助類來實現類似的東西。該輔助類擴展了Loader並提供圖像的自動縮放。以下是該類的代碼:

package {
import flash.display.Loader; import flash.geom.Rectangle; import flash.net.URLRequest; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; public class ImageLoader extends Loader { private var _imageURL:String; // URL of image private var _imageBoundary:Rectangle; // boundary rectangle for the image private var _loaded:Boolean; // flag which tells whether image is loaded or not. private var _isLoading:Boolean; // flag which say if any loading is in progress //Constructor function, which calls Loader's constructor // and loads and resize the image public function ImageLoader(url:String = null, rect:Rectangle = null):void { super(); _imageURL = url; _imageBoundary = rect; _loaded = false; _isLoading = false; loadImage(); } // sets the image for the loader and loads it public function set imageURL(url:String):void { _imageURL = url; loadImage(); } // sets the boundary of the image and resizes it public function set boundary(rect:Rectangle):void { _imageBoundary = rect; resizeImage(); } private function removeListeners():void { this.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete); this.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError); this.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); } private function onComplete(e:Event):void { _loaded = true; _isLoading = false; removeListeners(); resizeImage(); } //In case of error, we are not propogating the event private function onError(e:Event):void { e.stopImmediatePropagation(); removeListeners(); } // real loading goes here // it first closes and unloads the loader and // then loads the image private function loadImage():void { if (_isLoading) { trace("Some loading is in progess"); return; } try { this.close(); this.unload(); } catch(e:Error) { //discarded } if (!_imageURL) return; _loaded = false; _isLoading = true; this.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError); this.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); this.load(new URLRequest(_imageURL)); } // standard resizing function for image so that it's // aspect ratio is maintained. private function resizeImage():void { if (!_imageBoundary || !_loaded) return; var aspect:Number = width/height; var cAspect:Number = _imageBoundary.width/_imageBoundary.height; if (aspect <= cAspect) { this.height = _imageBoundary.height; this.width = aspect * this.height; } else { this.width = _imageBoundary.width; this.height = this.width/aspect; } this.x = (_imageBoundary.width-this.width)/2 + _imageBoundary.x; this.y = (_imageBoundary.height-this.height)/2 + _imageBoundary.y; } } }
您可以像這樣使用它:
var _imageLoader:ImageLoader = new ImageLoader(); 
_imageLoader.imageURL = " http://some-image-url "; 
_imageLoader.boundary = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight); // or whatever suits you 
ImageLoader擴展了Loader類,以便您可以通過Loader類監聽所有的事件派發。希望能幫助到你。

+0

不錯。我感謝幫助。 – jml 2009-12-29 07:57:14

+0

@bhups:我在這裏創建了一個新主題: http://stackoverflow.com/questions/1997655/how-to-get-a-stage-calculation-to-resize-an-image-wrtc你能幫我嗎我可能會錯過什麼?非常感謝您的幫助。 – jml 2010-01-04 05:32:14

+0

嗨再次bhups,我有一個問題,當loadImage()函數內部檢測到錯誤時。你能告訴我問題是什麼嗎?我發現(如果我追蹤)第一次加載圖像時發現錯誤。如果我放棄錯誤,它會加載,但如果我返回,圖像不會加載。所以對我來說,用這種方式重新加載沒什麼意義。應該有一個更清晰的定義,比如對_loaded進行檢查......但這也不起作用? :s – jml 2010-01-08 03:39:12

1

嘗試實例新的Loader,可能是試圖回收它是給你的問題

+0

這是常見的做法嗎?會引起延遲嗎? – jml 2010-01-15 17:47:54

+0

我試圖實例化一個新的,這似乎並沒有工作。 – jml 2010-01-15 20:27:06