简体   繁体   中英

Create a New Level in LibGDX Box2D Without Tiled Maps

I am not using a tiled map for a game I am creating that involves Box2D. I am having an issue with creating a new level when I run the following code:

// Scenario in which a win becomes true
if(ballFollower.overlapping(hole, true))
    {
        win = true;
        ballFollower.remove();
        ball.remove();
        currentLevel += 1;
        previousLevel = currentLevel - 1;
        Action fadeIn = Actions.sequence(Actions.alpha(0), Actions.show(), Actions.fadeIn(2),
                Actions.forever(Actions.sequence(Actions.color(new Color(1, 0, 0, 1), 1),
                        Actions.color(new Color(0, 0, 1, 1), 1))));
        winText.addAction(fadeIn);
        System.out.println(currentLevel);
    }

// If won, give an option to move onto the next level.
    if(win)
    {
        nextLevelLabel.setVisible(true);
        if(Gdx.input.isKeyPressed(Keys.ENTER))
        {
            // currentLevel increments by 1 once the level is won.
            game.setScreen(new ElevatorLevel(game, currentLevel));
        }
    }

In my constructor,

private int previousLevel = 0;
private int currentLevel = 1;
public ElevatorLevel(Game g, int level)
{  
    super(g, level);  
    currentLevel = level;
}

I am using the variable level in the parameter so that when the setScreen() method is used to either restart the current level or more onto the next level, it knows which one to go to/create since it will be currentLevel. However, I haven't gotten it to function properly because whenever I press enter once the game is won, it sends me back to the same starting level. Any tips on how I can create a NEW level that contains the SAME objects as my starting level?

I can post the code that the game begins with if requested, it just takes up lots of lines.

Turns out I had the logic wrong. I had a global variable,

private int mapHeight;

I had set it to 750 in my create method. The issue was that I was updating it according to currentLevel in the create method below. This caused it to either crash or the worlds height would always be messed because mapHeight would never make sense. What I did was instead of putting it in the create method, I left it as 750 and placed it in my update(float dt) method:

public void create() 
{   
    mapHeight = 750;
    // rest of code below etc
}
public void update(float dt)
{
   // Now, update mapHeight according to values in constructor.
   mapHeight = currentLevel * 750;
   // rest of code below etc
}

Since I did this, the map would initially start at 750, and then every time the level was beat and enter was pressed, it would update it properly. I'm not sure if I explained it correctly but it does work and make sense in my head :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM