Change the tilemap shown on stage in Flixel

I am building a simple platformer using Flixel, beginning with the source code from Flixel creator's EZPlatformer . I would like to adjust the level's tilemap when the player sprite enters/overlaps with a sprite representing a door. I use Flixel's built in function to track if the player overlaps the door sprite.

exits is a class level variable: public var exits:FlxGroup;

I call this function in the Update event to check for overlap: FlxG.overlap(exits,player,UpdateScreen);

I successfully catch when they overlap, however when I try and load the new tilemap, the old one continues to show. I use this function to attempt to update the tilemap on the screen:

public function ChangeScreen():void
{
//load the screen and locations of sprites(e.g. player, exits) based on current screen
switch(currentScreen)
{
case 1:
level = new FlxTilemap();
break;
case 2:
level = new FlxTilemap();
break;
}
}


I thought that I would need to first call remove on level to take it off the stage, and then call add on it, however, when I do that, the tilemap does not show up at all. What is the correct way to do this?

When you set level to a new value, you lose the reference to it in your code. Its parent FlxGroup still maintains a reference to it in its members array, however, so it continues to display. Using remove is a good idea, but I suspect you're doing it like this:

level = new FlxTilemap();
remove(level); //WRONG


You need to remove the level while your reference still points to the old level, like so:

remove(level);
level = new FlxTilemap();

• I tried rearranging in that order and it seems to work perfectly for what I needed. I can't believe I didnt' try that order, thank you so much. I was really frustrated before and eventually just implemented a workaround using multiple screen variables and set the exists member to false on the unused levels. Needless to say, this way is much cleaner. Dec 29 '12 at 2:08