2010-07-09 62 views
1

由於某些原因,這個flex 4代碼給了我一個錯誤,但我找不出原因。在我WindowedApplication我:Flex 4/WindowedApplication的空氣設置邊界

var prefs:Object = fs.readObject(); 
fs.close(); 
var rect:Rectangle = new Rectangle(); 
rect = prefs.bounds as Rectangle; 
this.bounds = rect; // error here 

Error message: ArgumentError: Error #2007: Parameter rect must be non-null.

我本來也試了一下沒有RECT對象,只是做:

this.bounds = prefs.bounds as Rectangle; 

使我有以下錯誤:

TypeError: Error #1034: Type Coercion failed: cannot convert [email protected] to flash.geom.Rectangle.

這似乎是一個虛假的錯誤,因爲我可以分配pref.bounds rect沒有錯誤。我不知道爲什麼這不起作用。它在flex 3兼容模式下工作,但也打破了我的許多spark組件,所以我不能使用它。

回答

0
rect = prefs.bounds as Rectangle; 

rect最終成爲null因爲prefs.bounds不是Rectangle類的一個實例。如果失敗,使用as關鍵字投射返回null

只要在readObject的電話後給出trace(prefs.bounds);以查看它包含的內容。


更新:

prefs.bounds值有X,Y,寬度和高度屬性的事實,並不能使它一個Rectangle對象 - 它只是那四個屬性的對象。最簡單的辦法是,由這些值創建一個Rectangle對象,並使用:

var prefs:Object = fs.readObject(); 
var rect:Rectangle = new Rectangle(prefs.bounds.x, prefs.bounds.y, prefs.width, prefs.height); 
this.bounds = rect; 

如果你寫的矩形對象文件流之前調用registerClassAlias,該readObject將返回相應類型的對象,而不是一個通用的一。

registerClassAlias("flash.geom.Rectangle", Rectangle); 
fs.writeObject(this.bounds); 

//later... 
//Casting with() will throw an error if the read object is not a Rectangle 
var rect:Rectangle = Rectangle(fs.readObject()); 
trace(rect); 
+0

所以你這樣說: this.bounds = rect; 等同於: this.bounds = null; 而windowedApp不接受? 當我使用調試器時,prefs.bound具有您期望的所有字段,寬度,高度,x,y等。因此,如果它不是矩形,我不知道它會是什麼。 通過這種方式的prefs對象實際上是從this.bounds最初生成並保存到一個文件。這就是爲什麼當我用這段代碼讀回來的時候,我感到很驚訝,它就像這樣窒息。 – solerous 2010-07-09 18:28:07

+0

@solerous看到我的更新。 – Amarghosh 2010-07-10 04:57:19

+0

嘿謝謝你的迴應。不幸的是,代碼不起作用。 var rect:Rectangle = Rectangle(fs.readObject()); (rect); trace(rect); 給我錯誤: TypeError:錯誤#1034:類型強制失敗:無法將Object @ 1db7bdf9轉換爲flash.geom.Rectangle。儘管我使用: registerClassAlias(「flash.geom.Rectangle」,Rectangle); fs.writeObject(this。邊界); 最初保存它。 我只是不明白爲什麼AIR不能做這種1:1轉換。這段代碼實際上是從Flex 3中的應用程序中獲取的,並且在那裏完美運行。我只是不知道Flex 4中發生了什麼變化。 – solerous 2010-08-02 20:20:33