2011-03-09 216 views
0

我正在從閃存中的圖像構建一個多維數據集。加載器加載圖像的精靈圖:這是爲什麼拋出錯誤?

+----++----++----++----++----++----+ 
|frnt||rght||back||left||top ||bot | 
| || || || || || | 
+----++----++----++----++----++----+ 

我得到的錯誤是:

錯誤#2005:參數0類型不正確的。應該是類型BitmapData。

在線路front.draw(src, null, null, null, clip);(由註釋代碼和接收到錯誤確認)。但我不知道爲什麼。我定義srcBitmapData,和一個src產生trace[object BitmapData]

private function loaderCompleteListener(e:Event):void 
{ 
    log('Complete'); 
    var front:BitmapData, 
    right:BitmapData, 
    back:BitmapData, 
    left:BitmapData, 
    top:BitmapData, 
    bottom:BitmapData, 
    srcData:Bitmap, 
    src:BitmapData, 
    clip:Rectangle; 

    try 
    { 
    srcData = this._imageLoader.content as Bitmap; 
    src = srcData.bitmapData; 
    front = new BitmapData(src.height, src.height, false, 0); 
    right = new BitmapData(src.height, src.height, false, 0); 
    back = new BitmapData(src.height, src.height, false, 0); 
    left = new BitmapData(src.height, src.height, false, 0); 
    top = new BitmapData(src.height, src.height, false, 0); 
    bottom = new BitmapData(src.height, src.height, false, 0); 
    clip = new Rectangle(0, 0, src.height, src.height); 

    //This is the line that causes the error 
    front.draw(src, null, null, null, clip); //<---- 
    clip.x += src.height; 
    right.draw(src, null, null, null, clip); 
    clip.x += src.height; 
    back.draw(src, null, null, null, clip); 
    clip.x += src.height; 
    left.draw(src, null, null, null, clip); 
    clip.x += src.height; 
    top.draw(src, null, null, null, clip); 
    clip.x += src.height; 
    bottom.draw(src, null, null, null, clip); 
    } 
    catch (e:Error) 
    { 
    log(e.message); 
    } 
} 

我缺少什麼?

修改即可添加:

一個考慮因素可能是圖像大小。我正在加載1866年的圖片× 11196.我將在明天測試以查看較小的圖片是否有效。這可能就是Flash無法處理超過一定大小的圖像。

+0

你有一個體面的調試器?如果是這樣,你可以在srcData = this._imageLoader.content中設置一個斷點作爲Bitmap;並檢查src是否實際上被分配了BitmapData對象? –

+0

@EyeSeeEm,'log'是一個定製的JavaScript記錄器,默認爲flash編輯器中的trace。我可以正確讀取'src.height'和'src.width',所以我假定它被正確分配。 – zzzzBov

回答

0

錯誤實際上是'參數0是不正確的類型。應該是類型IBitmapDrawable' - 不是BitmapData。 (無論如何,在我的機器上)。

但是BitmapData確實是IBitmapDrawable - 問題在於事實上,一個類型爲某個類的對象沒有被構造,或者等於null實際上並沒有實現任何東西。

// Example 
var src:BitmapData; 
trace(src is IBitmapDrawable); // false 

在你的情況,如果srcData(Bitmap對象)不具有的位圖數據也將返回null和src(中BitmapData)設置爲null。所以當null傳遞給draw()時,它會嚇倒,因爲null不是IBitmapDrawable。

例如,如果我做到以下幾點:

var o:BitmapData = new BitmapData(100, 100, false, 0); 
o.fillRect(new Rectangle(0, 0, 100, 100), 0x334455); 

var srcData:Bitmap = new Bitmap(o); 
var src:BitmapData; 

src = srcData.bitmapData; 

trace(src is IBitmapDrawable); 

var front:BitmapData = new BitmapData(100, 100, false, 0); 
front.draw(src as IBitmapDrawable); // works! 

但如果我刪除srcData O(的BitmapData),如:你的情況如此反覆,

var srcData:Bitmap = new Bitmap(); 
var src:BitmapData; 

src = srcData.bitmapData; 

trace(src is IBitmapDrawable); 

var front:BitmapData = new BitmapData(100, 100, false, 0); 
front.draw(src as IBitmapDrawable); // Doesn't work! 

,位圖對象沒有收到任何輸入數據。

+0

如果src爲null,他怎麼能調用src.height和.width上面的幾行,而不是得到空指針異常? –

+0

一個額外的考慮,我會追加到我的答案:我試圖加載的圖像非常大(1866 x 11196);可能是閃存無法管理大型圖像。 – zzzzBov

+0

良好的拾取EyeSeeEm。 @zzzzBov可以在.draw行之前放置以下內容。 'trace(src is IBitmapDrawable);' – Chris

2

正如克里斯指出,內置最大的高度或寬度爲8192對任何的BitmapData,所以你將不能夠使用本地的圖像是在寬度11196。

但是,有解決方法,如BitmapDataUnlimited類,您可以找到here

1

首先,我需要感謝@Chris,@EyeSeeEm,@Nathan Ostgard,因爲我導致了我收到錯誤的原因。我的期望是,如果圖片加載不正確,我會收到一個IOErrorEvent.IO_ERROR(這對我來說看起來像是一個IO錯誤),但是Flash顯然並沒有認爲這一點足夠了,這就是爲什麼我得到一個虛假錯誤。

正如大家指出的那樣,AS3文檔指定圖像不能超過特定的大小。

Flash播放器9:

BitmapData對象的最大寬度和最大高度爲2880個像素。

Flash播放器10:

在AIR 1.5和Flash播放器10中,BitmapData對象的最大尺寸的寬度或高度8191個像素,並且像素的總數不能超過16,777,215像素。 (因此,如果BitmapData對象寬8,191像素,則它只能高2048像素。)在Flash Player 9及更早版本和AIR 1.1及更早版本中,高度限制爲2,880像素,寬度限制爲2,880。

但最重要的原因我發現了一個ArgumentError似乎沒有什麼意義是在CS4 docs下一行:

呼籲爲BitmapData的任何方法或屬性如果BitmapData對象無效(例如,它具有height == 0width == 0),或者它已通過dispose()進行處理,則會拋出ArgumentError錯誤。

因爲我的圖像比8191px更寬,所以它被認爲是無效的,導致難以理解的ArgumentError

這在調用draw時很有意義,但仍不能解釋爲什麼我可以在不觸發錯誤的情況下調用src.height

+0

我認爲這可能是兩回事。如果圖像太大,draw方法可能會拋出ArgumentError,而.height屬性只會拋出錯誤,其高度爲0。 –

相關問題