3
\$\begingroup\$

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:
                    //load tilemap
                    level = new FlxTilemap();
                    level.loadMap(FlxTilemap.arrayToCSV(data1,40),FlxTilemap.ImgAuto,0,0,FlxTilemap.AUTO);
                    add(level);
                    break;
                case 2:
                    //load new tilemap
                    level = new FlxTilemap();
                    level.loadMap(FlxTilemap.arrayToCSV(data2,40),FlxTilemap.ImgAuto,0,0,FlxTilemap.AUTO);
                    add(level);
                    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?

\$\endgroup\$
2
3
\$\begingroup\$

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();
level.loadMap(FlxTilemap.arrayToCSV(data2,40),FlxTilemap.ImgAuto,0,0,FlxTilemap.AUTO);
remove(level); //WRONG
add(level);

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

remove(level);
level = new FlxTilemap();
level.loadMap(FlxTilemap.arrayToCSV(data2,40),FlxTilemap.ImgAuto,0,0,FlxTilemap.AUTO);
add(level);

Also, make sure that you're removing the old level from the same FlxGroup that you added it to.

\$\endgroup\$
3
  • \$\begingroup\$ Ok, thank you for the assistance. That sounds logical, but I think I tried that once before. I will try it again though. However, you say, "make sure you're removing the old level from the same FlxGroup that you added it to". This may be a dumb question, but won't the function call add() , when not called specific to a FlxGroup, reference the global group for the FlxGame? i.e. always reference the same group? or is there something I am missing? \$\endgroup\$
    – Christian
    Dec 29 '12 at 2:03
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Christian
    Dec 29 '12 at 2:08
  • \$\begingroup\$ A plain add() is the same as this.add(); it will add the new member to the current instance of whatever class your function is defined in. \$\endgroup\$ Jan 1 '13 at 5:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .