-
[RESOLVED] nested array does not work
Hi folks,
I have the following problem: I have this multi-level array (nested array) which contains two rows of bitmapData. Row 1:360 rotated bitmapData objects; row 2: 360 rotated and colored bitmapData objects.
I try to access row 2 but that doesn't work. There are some mysterious error messages coming up ("TypeError: Error #1034: Type Coercion failed: cannot convert []@36d7e9e9 to flash.display.BitmapData. at BasicBlitArrayObject/updateFrame()").
Please can someone help me out with this problem? Thank you very much.
this function rotates and colors bitmapData; the rotated bitmapData is thrown into an array and the colored bitmapData is thrown into another array; a third array is used as a level array for nesting the other two arrays inside of it
Code:
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);
}
creating my rotated and colored bitmapData
Code:
animationFrames = tempBlitArrayAsset.createRotationBlitArrayFromBD($bitmapData, 1, 270);
here I try to access the first row of my level array (that doesn't work; I can't access it)
Code:
tempEnemy.animationList = animationFrames;
tempEnemy.bitmapData = tempEnemy.animationList[1][tempEnemy.frame];
This function is for updating the frames
Code:
public function updateFrame(inc:int, row:int = 0):void
{
frame += inc;
if (frame > animationList.length - 1){
frame = 0;
}
bitmapData = animationList[row][frame];
}
}
this is a line showing how the updateFrame-function is used in my game (trueRotation is 0)
Code:
tempEnemy.updateFrame(tempEnemy.trueRotation);
-
I dont see anything immediate. There is some optimization to your generation loop that may help - try something like this:
PHP Code:
public function createColoredRotationBlitArrayFromBD(sourceBitmapData:BitmapData, inc:int, offset:int = 0, color:uint = 1, _alpha:Number = 1):Array{
const levelArray:Array = [[], []];
// const levelArray:Vector.<Vector.<BitmapData>> = new Vector.<Vector.<BitmapData>>(2, true);
// a vector would be faster here and also let you know if you are putting
// in bad bitmapdata before you get downstream to the rest of the code...
// but would require some changes outside this function
var rotation:int = offset;
// you can zero this matrix out in each iteration - faster than allocating a new one each time
const rotationMatrix:Matrix = new Matrix();
// since this doesn't change = you can re-use the same filter for every iteration
const colorMatrix:ColorMatrixFilter = new ColorMatrixFilter([color, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, $alpha, 0]);
while(rotation < (360 + offset)){
var angleInRadians:Number = Math.PI * 2 * (rotation / 360);
rotationMatrix.identity(); // reset the 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);
levelArray[0].push(matrixImage.clone());
trace('levelArray[0][' + levelArray[0].length-1 + '] = ' + levelArray[0][levelArray.length-1]);
// since you clone matrixImage above - yo can now alter it without affecting what's already saved in the array
matrixImage.applyFilter(matrixImage, matrixImage.rect, point0, colorMatrix);
// no need to clone - done with this bitmapdata after this
levelArray[1].push(matrixImage);
trace('levelArray[1][' + levelArray[1].length-1 + '] = ' + levelArray[1][levelArray.length-1]);
rotation += inc;
// no need to zero out the vars here - they all get overwritten in the next iteration
}
return(levelArray);
}
That has some traces so you can make sure all your bitmaps are coming in correctly (be careful to look at the ends of the arrays for any nulls or anything odd).
If that doesn't turn anything up, you can add some heavy duty checking on the updateFrame:
PHP Code:
if(row in animationList){
if(frame in animationList[frame]){
if(animationList[row][frame] is BitmapData){
bitmapData = animationList[row][frame] as BitmapData;
} else {
trace('invalid animation cel - expecting BitmapData: animationList[' + row + '][' + frame + '] = ' animationList[row][frame]);
}
} else {
trace('could not find a frame in the animations!: animationList[' + row + '][' + frame + ']');
}
} else {
trace('could not find an entire tier of the animations!!: animationList[' + row + ']');
}
Please use [php] or [code] tags, and mark your threads resolved 8)
-
Wow! Thank you very much! But I think I discovered an error in your optimization in the following line of code (call me an idiot if I'm wrong):
Code:
matrixImage.applyFilter(matrixImage, matrixImage.rect, point0, colorMatrix);
( from adobe docs for BitmapData.applyFilter() http://help.adobe.com/en_US/FlashPla...ml#applyFilter)
If the BitmapData object and the object specified as the sourceBitmapData parameter are the same object, the application uses a temporary copy of the object to perform the filter. For best performance, avoid this situation.
That was the reason why I created a third bitmapData-object to bypass this problem.
Last edited by drpelz; 09-11-2011 at 12:02 PM.
-
I notice that in the original posted code, tileList, tileListSec, and levelArray are NOT local variables in createColoredRotationBlitArrayFromBD. Although you are setting them to new arrays within the function. I would be wary of scope issues. There's not reason not to make them local variables to ensure that they won't be messed with from other places.
-
Thanks for the tip! Although they are no local variables it works fine and I had absolutely no issues until now. The array is working and all bugs are gone now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|