Log in
Please log in or register.
Pages: [1]
  Print  
Author Topic: Dynamically changing a FlxTilemap  (Read 838 times)
nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« on: Mon, Aug 31, 2009 »

This is an update to the tilemap class to be able to change and check the tiles with some functions (which I'm using in Caverns).

Besides the enhancement of being able to dynamically change the tilemap, you can save memory because you'll be saving the same number of rects (_rects) as tile types in the tileset against saving as many rects as tiles in the map (imagine a 200x100 tilemap!, 20000 rects against, 30 tops?)

Maybe it was done this way because of an optimization? I don't know, but AFAIK they have the same performance.

Good luck!
-Martín


I did it easily like this: ( //////////// means removed)

@ class definition
Code:
////////////private var _rects:FlxArray;
private var _rectsPerType:Array;
private var _drawIndex:uint;

@ constructor
Code:
//////////// for(var i:uint = 0; i < numTiles; i++)
//////////// {
//////////// if(_data[i] >= DrawIndex)
//////////// _rects.push(new Rectangle(_tileSize*_data[i],0,_tileSize,_tileSize));
//////////// else
//////////// _rects.push(null);
//////////// }

_drawIndex = DrawIndex;
_rectsPerType = new Array;
for(i = 0; i < _pixels.width / _pixels.height; i++)
{
_rectsPerType.push(new Rectangle(_tileSize*i,0,_tileSize,_tileSize));
}

@ render()
Code:
//////////// if(_rects[cri] != null)
//////////// FlxG.buffer.copyPixels(_pixels,_rects[cri],_p,null,null,true);

var tile:uint = _data[c+r*widthInTiles];
if(tile >= _drawIndex)
FlxG.buffer.copyPixels(_pixels,_rectsPerType[tile],_p,null,null,true);


And then an set/get pair of functions that change "_data".

Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
Tyraelic
Newbie
*
Posts: 10


View Profile
« Reply #1 on: Mon, Aug 31, 2009 »

Awesome, am totally gonna have to try this out when i get a chance.  Oh and i love caverns btw, cheers for the link, been dieing to play the ludum dare entries but i didnt have the confidence to enter it yet so am just gonna have to wait.  Maybe enter the next one!
Logged
Vexhel
Newbie
*
Posts: 18


Huh?


View Profile
« Reply #2 on: Mon, Sep 7, 2009 »

I was looking for something like this. Nice work, although I found out that to make it work with a scrolling background (ie: with a tilemap larger than the current screen) without glitches, you have to correct a little thing: in the render function you have to substitute
Code:
var tile:uint = _data[c+r*widthInTiles];
with
Code:
var tile:uint = _data[(ty+r)*widthInTiles+tx+c];
Logged

nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« Reply #3 on: Tue, Sep 8, 2009 »

Ohhh I see, you're right (didn't see it because my window doesn't scroll), thanks for fixing that up!
Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
Pages: [1]
  Print  
 
Jump to: