2011-09-10 128 views
0

我有以下問題:我有這個多級數組(嵌套數組),其中包含兩行bitmapData。第1行:360位旋轉的bitmapData對象;第2行:360旋轉和彩色的bitmapData對象。嵌套數組不工作

我嘗試訪問第2行,但不起作用。有一些神祕的錯誤消息(「TypeError:Error#1034:Type Coercion failed:can not [] @ 36d7e9e9 to flash.display.BitmapData。at BasicBlitArrayObject/updateFrame()」)。

請問有人可以幫我解決這個問題嗎?非常感謝你。

此函數旋轉並着色bitmapData;旋轉後的bitmapData被拋入一個數組中,彩色的bitmapData被拋入另一個數組中;第三陣列用作級陣列中嵌套它

public function  createColoredRotationBlitArrayFromBD(sourceBitmapData:BitmapData, inc:int,  offset:int = 0, color:Number = 1, $alpha:Number = 1):Array 
{ 

tileList = []; 
tileListSec = []; 
levelArray = [tileList, tileListSec]; 
var rotation:int = offset; 

while (rotation < (360 + offset)) 
{ 
    var angleInRadians:Number = Math.PI * 2 * (rotation/360); 
    var rotationMatrix:Matrix = new Matrix(); 

    rotationMatrix.translate(-sourceBitmapData.width * .5, -sourceBitmapData.height * .5); 
    rotationMatrix.rotate(angleInRadians); 
    rotationMatrix.translate(sourceBitmapData.width * .5, sourceBitmapData.height * .5); 


    var matrixImage:BitmapData = new BitmapData(sourceBitmapData.width, sourceBitmapData.height, 
           true, 0x00000000); 

    matrixImage.draw(sourceBitmapData, rotationMatrix); 
    tileList.push(matrixImage.clone()); 


    bitmapData = new BitmapData(matrixImage.width, matrixImage.height, true, 0x00000000); 
    bitmapData = matrixImage; 


    var colorMatrix:ColorMatrixFilter = new ColorMatrixFilter (
         [color, 0, 0, 0, 0, 
         0, 0, 0, 0, 0, 
         0, 0, 0, 0, 0, 
         0, 0, 0, $alpha, 0]); 

    matrixImage.applyFilter(bitmapData, bitmapData.rect, point0, colorMatrix); 


    tileListSec.push(matrixImage.clone()); 


    rotation += inc; 

    matrixImage.dispose(); 
    matrixImage = null; 
    rotationMatrix = null; 
    bitmapData.dispose(); 
    bitmapData = null; 
    colorMatrix = null; 
    } 

return(levelArray); 

} 

創建我的旋轉和有色位圖數據

animationFrames = tempBlitArrayAsset.createRotationBlitArrayFromBD($bitmapData, 1, 270); 

這裏的其他兩個數組我嘗試訪問我的水平陣列的第一行(即不工作;我不能訪問它)

tempEnemy.animationList = animationFrames; 
tempEnemy.bitmapData = tempEnemy.animationList[1][tempEnemy.frame]; 

該功能是用於更新幀

public function updateFrame(inc:int, row:int = 0):void 
{ 
frame += inc; 

if (frame > animationList.length - 1){ 
    frame = 0; 
} 
bitmapData = animationList[row][frame];     


} 

} 

這是顯示updateFrame功能是如何在我的遊戲中使用的線路(trueRotation爲0)

tempEnemy.updateFrame(tempEnemy.trueRotation); 
+0

它看起來像一個數組越來越推到你期望的位圖數據。您是否嘗試過使用強類型向量而不是數組。如果你知道它總是會有bitmapdatas,那麼它是有道理的,當你在那裏放置一些沒有意義的東西時它會拋出一個運行時異常。我會在後面更詳細地看代碼,看看我是否能夠確切地知道它發生在哪裏 –

+0

不,我還沒有嘗試過一個向量。我個人認爲這不是問題。儘管如此,我會稍後再嘗試... – drpelz

回答

1

我找不到什麼毛病createColoredRotationBlitArrayFromBD

var $bitmapData:BitmapData = new BitmapData(40,40,false, 0x7f7f7f); 
var animationFrames:Array = createColoredRotationBlitArrayFromBD($bitmapData, 1, 270); 
trace(animationFrames.length); // 2 
trace(animationFrames[0].length); // 360 
trace(animationFrames[1].length); // 360 

var bitmap:Bitmap = new Bitmap(); 
this.addChild(bitmap); 
bitmap.bitmapData = animationFrames[1][0]; // works.. 

這似乎正確。對?我得到一個紅色的有點位圖。

唯一的 '錯誤' 我在你列出的代碼看到的是updateFrame

if (frame > animationList.length - 1){ 
    frame = 0; 
} 

也許應該是:

if (frame > animationList[row].length - 1){ 
    frame = 0; 
} 

因爲animationList.length == 2

,但一切看起來不錯的你提供的代碼,所以沒有更多的代碼,我不確定是否有任何幫助。

+0

正是!那是錯誤。我2天前發現了這個bug,現在工作正常。感謝您的提示!:) – drpelz

+0

更改此行「if(frame> animationList.length - 1){」to this「if(frame> animationList [row] .length - 1){ 」。 – drpelz