Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
1  
What is the code in add(level)? –  Gone3d Dec 28 '12 at 14:27
1  
add() is provided by the framework and can be seen at github.com/AdamAtomic/flixel/blob/master/org/flixel/… –  Gregory Avery-Weir Dec 28 '12 at 19:50

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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? –  Christian Dec 29 '12 at 2:03
1  
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. –  Christian Dec 29 '12 at 2:08
    
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. –  Gregory Avery-Weir Jan 1 '13 at 5:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.