Rihards
Rihards

Reputation: 10339

How to avoid create new bitmapData object in loop in AS3?

I want to store the bitmap data from _sampleTile in array, but I was wondering how to increase the performance. If I do it like this:

var _sampleTile:BitmapData;
var _arrayLenght:int = _tileClipArray.length;
for(var i:int = 0; i < _arrayLenght; ++i){
    _sampleTile = new BitmapData(65, 65, false);
 _sampleTile.draw(_tileClipArray[int(i)]);
 _tileBitmapDataArray[i] = _sampleTile;
}

Then it would do too much constructing job in the loop, right? But if I do as bellow:

var _sampleTile:BitmapData = new BitmapData(65, 65, false);
var _arrayLenght:int = _tileClipArray.length;
for(var i:int = 0; i < _arrayLenght; ++i){
 _sampleTile.fillRect(_sourceRectangle, 0x00FFFFFF);
 _sampleTile.draw(_tileClipArray[int(i)]);
 _tileBitmapDataArray[i] = _sampleTile.clone();
}

The .clone() returns a new BitmapData object so basically the result is the same, right? In the second example if we replace the _sampleTile.clone() with _sampleTile - is it somehow possible to not store in array a reference to _sampleTile, but get the actual bitmapData from the _simpleTile?

Upvotes: 0

Views: 628

Answers (1)

Cay
Cay

Reputation: 3794

No, you need to create a new BitmapData each iteration... either with clone() or new.

I see a couple alternatives though:

  • Make your creation asynchronous. Do just a few each frame, till you finish the whole batch.
  • Create a big BitmapData, draw all tiles in there and use references for the position of each tile. If the tiles are always the same, then you could eventually save the final BitmapData + positions and load them instead of creating them each time you run the application.

Upvotes: 1

Related Questions