Switching levels


(B6ka) #1

Hi,

I would like to make a minigame in the middle of one of the levels. I am thinking of implementing the minigame as a separate world class (this seems the most convenient way). After the minigame is completed the player should be returned to exactly the same level and spot where he was when the minigame started; the enemies or other game items should be exactly the same. When I try a simply:

FP.world = level1

where level1 is an instance of the Level world, it reloads everything in the starting state; i.e. all enemies are alive, etc.

Is there an easy way to remember the state of the level and come back to it when you switch the worlds? Thank in advance for suggestions (and saving my time) :slight_smile:


(Bora Kasap) #2

there is no any easy ways to save your world’s state, you have to save everything one by one… so, my idea, it is better you don’t use another world for a mini game, so, why you need a new world for a “minigame” ?


(David Williams) #3

You could try storing the current world in a variable.

public var storedWorld:World;
...
storedWorld = FP.world;
FP.world = new MinigameWorld();
...
FP.world = storedWorld();

Not sure how well it will work, but worth a shot, right?


(Bora Kasap) #4

Are you serious! Holy god! I feel so depressed right now because of lost times for trying something similiar! Why nobody talked about that before :frowning: that’s one of the most important features in fp… poor alobar…


(Jacob Albano) #5

What I usually do in this situation is actually pass the original world into the new world’s constructor. That way it has a reference so it can go back.

FP.world = new MinigameWorld(FP.world);

//  reference to the previous world
private var prevWorld:World;

// constructor
public function MinigameWorld(prevWorld:World)
{
    this.prevWorld = prevWorld;
}

public function goBack()
{
    FP.world = prevWorld;

    // for extra fun, add a quick flash animation on top of everything
    var flash = Image.createRect(FP.width, FP.height, 0xffffff);
    prevWorld.addGraphic(flash, -9001);
			
    var tween = new VarTween(null, ONESHOT);
    tween.tween(flash, "alpha", 0, 0.25, Ease.sineOut);
    prevWorld.addTween(tween, true);
}


Keeping the values of variables while world changes
(B6ka) #6

@AlobarNon Thanks. It would be difficult to implement the minigame inside a level.

@Deamonr Thanks for suggestion, this is what I tried before posting here. If you do it this way, you get two instances of player, enemies, etc. One instance in the state before switching the world and the other in creation state. One way around this would be to check if an instance of a given class is already added.

			if (this.classCount(Player) == 0)
		{
			player = new Player(100, 50);
			add(player);
		}

This is a bit cumbersome for many entities, and also I am not sure if there are other variables behind the scenes that are duplicated and would lower the performance. Hence my request for advice from knowledgeable people here who know the specifics of FP :).

@jacobalbano Thanks, will try this :slight_smile:


(B6ka) #7

@jacobalbano Thanks, your approach seems to work very well for me. If you do not need to save data for many levels but just a few previous ones, then you can pass them in the constructor and restore them back with the state that you left easily with FP.world = savedLevel.