2010-06-08 58 views
0

我試圖訪問通過自定義LoadImage類添加到舞臺的圖像的寬度和高度。即使圖像正確顯示,跟蹤結果也是0。問題是什麼?ActionScript正在檢索裝載程序圖像的寬度/高度

//frame script 

var image:LoadImage = new LoadImage("myImage.jpeg"); 
addChild(image); 
trace(image.width); //returns 0 

//------------------------- 

package 
{ 
import flash.display.Sprite; 
import flash.display.Loader; 
import flash.net.URLRequest; 
import flash.events.Event; 

public class LoadImage extends Sprite 
    { 
    public function LoadImage(imageURL:String) 
     { 
     //Load Image 
     var imageLoader:Loader = new Loader(); 
     imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageHandler); 
     imageLoader.load(new URLRequest(imageURL)); 
     } 

    private function imageHandler(evt:Event):void 
     { 
     addChild(evt.target.content); 
     } 
    } 
} 
+0

嘗試在完整的處理程序 – 2010-06-08 14:32:00

+0

我需要從我的框架腳本訪問寬度之類的外跟蹤寬度。 – TheDarkIn1978 2010-06-08 14:38:48

回答

1

如果您在實例化後立即嘗試訪問它,則無法訪問其屬性。你將不得不讓事件驅動:

class LoadImage loads image 
frame script listens for a complete event from LoadImage 
LoadImage loads the image, once it has it's hands on it it dispatches the event 
frame script works with the data 

你需要做的的LoadImage事件,一次在imageHandler完成,派遣這一點。當你讓你的新的LoadImage,建立監聽

var image:LoadImage = new LoadImage("myImage.jpeg"); 
image.addEventListener("complete", loadedImage); //or whatever you call the event 

function loadedImage(evt:Event) { 
    addChild(evt.target); 
    trace(evt.target.content.width); //returns 0 
} 
+0

evt.target.content.width工作正常,但我的問題是從框架腳本訪問此,而不是從類內 – TheDarkIn1978 2010-06-08 14:38:07

+0

這就像送你的女朋友到商店的啤酒,立即去冰箱,並期待在那裏。你必須等她回來把啤酒放進冰箱裏。 (請參閱新的答案) – 2010-06-08 15:01:36

0

您試圖在圖像完全加載之前獲取寬度和高度?有關更多詳細信息,請參閱this question