[英]Create a New Level in LibGDX Box2D Without Tiled Maps
我沒有為正在創建的涉及Box2D的游戲使用平鋪地圖。 運行以下代碼時,在創建新級別時遇到問題:
// 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));
}
}
在我的構造函數中
private int previousLevel = 0;
private int currentLevel = 1;
public ElevatorLevel(Game g, int level)
{
super(g, level);
currentLevel = level;
}
我在參數中使用變量級別 ,以便在使用setScreen()方法將當前級別或更高級別重新啟動到下一個級別時,它知道要轉到/創建哪個級別,因為它將是currentLevel。 但是,我沒有使它正常運行,因為一旦贏得比賽,每當我按Enter鍵,它就會使我回到相同的起始水平。 關於如何創建包含SAME對象作為起始級別的NEW級別的任何提示?
如果需要,我可以發布游戲開始的代碼,它只占用很多行。
原來我的邏輯是錯誤的。 我有一個全局變量
private int mapHeight;
我在創建方法中將其設置為750。 問題是我正在根據下面的create方法中的currentLevel更新它。 這導致它崩潰或世界高度總是被弄亂,因為mapHeight永遠沒有意義。 我所做的不是將其放在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
}
既然我這樣做了,地圖最初將在750開始,然后每次敲打關卡並按Enter時,它將正確更新。 我不確定我是否正確解釋了它,但是它確實起作用並且在我腦海中是有意義的:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.