Animating Tile with Blitting taking up Memory.
Posted
by
Kid
on Game Development
See other posts from Game Development
or by Kid
Published on 2011-06-26T05:01:24Z
Indexed on
2011/06/26
8:30 UTC
Read the original article
Hit count: 399
I am trying to animate a specific tile in my 2d Array, using blitting. The animation consists of three different 16x16 sprites in a tilesheet. Now that works perfect with the code below. BUT it's causing memory leakage. Every second the FlashPlayer is taking up +140 kb more in memory. What part of the following code could possibly cause the leak:
//The variable Rectangle finds where on the 2d array we should clear the pixels
//Fillrect follows up by setting alpha 0 at that spot before we copy in nxt Sprite
//Tiletype is a variable that holds what kind of tile the next tile in animation is
//(from tileSheet)
//drawTile() gets Sprite from tilesheet and copyPixels it into right position on canvas
public function animateSprite():void{
tileGround.bitmapData.lock();
if(anmArray[0].tileType > 42){
anmArray[0].tileType = 40;
frameCount = 0;
}
var rect:Rectangle = new Rectangle(anmArray[0].xtile * ts, anmArray[0].ytile * ts, ts, ts);
tileGround.bitmapData.fillRect(rect, 0);
anmArray[0].tileType = 40 + frameCount;
drawTile(anmArray[0].tileType, anmArray[0].xtile, anmArray[0].ytile);
frameCount++;
tileGround.bitmapData.unlock();
}
public function drawTile(spriteType:int, xt:int, yt:int):void{
var tileSprite:Bitmap = getImageFromSheet(spriteType, ts);
var rec:Rectangle = new Rectangle(0, 0, ts, ts);
var pt:Point = new Point(xt * ts, yt * ts);
tileGround.bitmapData.copyPixels(tileSprite.bitmapData, rec, pt, null, null, true);
}
public function getImageFromSheet(spriteType:int, size:int):Bitmap{
var sheetColumns:int = tSheet.width/ts;
var col:int = spriteType % sheetColumns;
var row:int = Math.floor(spriteType/sheetColumns);
var rec:Rectangle = new Rectangle(col * ts, row * ts, size, size);
var pt:Point = new Point(0,0);
var correctTile:Bitmap = new Bitmap(new BitmapData(size, size, false, 0));
correctTile.bitmapData.copyPixels(tSheet, rec, pt, null, null, true);
return correctTile;
}
© Game Development or respective owner