简体   繁体   English

在没有平铺地图的LibGDX Box2D中创建新级别

[英]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. 没有为正在创建的涉及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. 我在参数中使用变量级别 ,以便在使用setScreen()方法将当前级别或更高级别重新启动到下一个级别时,它知道要转到/创建哪个级别,因为它将是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. 但是,我没有使它正常运行,因为一旦赢得比赛,每当我按Enter键,它就会使我回到相同的起始水平。 Any tips on how I can create a NEW level that contains the SAME objects as my starting level? 关于如何创建包含SAME对象作为起始级别的NEW级别的任何提示?

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. 我在创建方法中将其设置为750。 The issue was that I was updating it according to currentLevel in the create method below. 问题是我正在根据下面的create方法中的currentLevel更新它。 This caused it to either crash or the worlds height would always be messed because mapHeight would never make sense. 这导致它崩溃或世界高度总是被弄乱,因为mapHeight永远没有意义。 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: 我所做的不是将其放在create方法中,而是将其保留为750并将其放在我的update(float dt)方法中:

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. 既然我这样做了,地图最初将在750开始,然后每次敲打关卡并按Enter时,它将正确更新。 I'm not sure if I explained it correctly but it does work and make sense in my head :) 我不确定我是否正确解释了它,但是它确实起作用并且在我脑海中是有意义的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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