简体   繁体   English

LibGDX如何在已经存在的价值上增加价值?

[英]LibGDX How can I add value on already existing value?

I have a game and when you complete a level you earn 25 gold. 我有一个游戏,当您完成一个关卡后,您将获得25金。 What I want is that it can stack so if I play again and earn 25 gold I'll now have 50 gold. 我想要的是它可以堆叠,因此,如果我再次玩并获得25金币,我现在将拥有50金币。 I have this but I can't stack, the game only remember the 25 gold from the current game: 我有这个,但我不能堆叠,游戏只记得当前游戏中的25金:

public static void addgold(int value){
    gold += value;
    gLabel.setText(String.format("%01d", gold));

    prefs.getInteger("gold", 0);
    prefs.putInteger("goldCoin", gold);
    prefs.flush();
}

I think I should write something like: 我想我应该写一些类似的东西:

goldCoin + gold

goldCoin is what I have earned in total and gold is what I get from the current game. goldCoin是我的总收入,而gold是我从当前游戏中获得的收入。

,

Here is all of the code: 这是所有代码:

public class Hud2 implements Disposable {
private Hud2 hud;
public Stage stage;
private boolean timeUp;
private Viewport viewport;

private Integer worldTimer;
private float timeCount;
private static Integer score;
private static Integer gold;

private boolean keyPressed = false;
private Runner player;
private static RunningGame game;

private TweenManager tweenManager;
public Box2DDebugRenderer b2dr;



private static Label scoreLabel;
private static Label timeLabel;
private static Label gLabel;
private Label levelLabel;
private Label worldLabel;
private Label runLabel;
private Label countdownLabel;
private Label objectiveLabel;


private static int hScore;
private static int gCoin;
private static Preferences prefs;



public Hud2(SpriteBatch sb) {
    worldTimer = 10;
    timeCount = 0;
    score = 0;
    gold = 0;





    viewport = new FitViewport(RunningGame.V_WIDTH, RunningGame.V_HEIGHT, new OrthographicCamera());
    stage = new Stage(viewport, sb);

    Table table = new Table();
    table.top();
    table.setFillParent(true);




    prefs = Gdx.app.getPreferences("PreferenceName");
    hScore = prefs.getInteger("highScore", 0);
    //prefs.putInteger("highScore", 0);

    prefs = Gdx.app.getPreferences("PreferenceGold");
    gCoin = prefs.getInteger("goldCoin", 0);
    //prefs.putInteger("goldCoin", 0);

    countdownLabel = new Label(String.format("%01d", worldTimer), new Label.LabelStyle(new BitmapFont(), com.badlogic.gdx.graphics.Color.WHITE));
    scoreLabel = new Label((String.format("%01d", score)), new Label.LabelStyle(new BitmapFont(), com.badlogic.gdx.graphics.Color.WHITE));
    gLabel = new Label((String.format("%01d", gCoin)), new Label.LabelStyle(new BitmapFont(), com.badlogic.gdx.graphics.Color.WHITE));
    timeLabel = new Label((String.format("%01d", hScore)), new Label.LabelStyle(new BitmapFont(), com.badlogic.gdx.graphics.Color.WHITE));
    levelLabel = new Label("", new Label.LabelStyle(new BitmapFont(), com.badlogic.gdx.graphics.Color.WHITE));
    worldLabel = new Label("Level 2", new Label.LabelStyle(new BitmapFont(), com.badlogic.gdx.graphics.Color.WHITE));
    runLabel = new Label("You won!", new Label.LabelStyle(new BitmapFont(), com.badlogic.gdx.graphics.Color.RED));
    objectiveLabel = new Label("Objective: 60", new Label.LabelStyle(new BitmapFont(), com.badlogic.gdx.graphics.Color.WHITE));



    table.add(worldLabel).expandX().padTop(10);
    table.add(gLabel).expandX().padTop(10);
    table.add(countdownLabel).expandX().padTop(10);
    table.row();
    table.add(timeLabel).expandX().padTop(10);
    table.add(levelLabel).expandX();
    table.row();
    table.add(timeLabel).expandX();
    table.add(scoreLabel).expandX();
    table.add(objectiveLabel).expandX().padTop(10);


    stage.addActor(table);
}




public void update(float dt) {
    if (score < 1) {
        return;
    }
    timeCount += dt;
    if (timeCount >= 1) {
        if (worldTimer > 0) {
            worldTimer--;
        } else {
            timeUp = true;
        }
        countdownLabel.setText(String.format("%03d", worldTimer));
        timeCount = 0;





        }

        Table table = new Table();
        table.top();
        table.setFillParent(true);
    if (worldTimer == 0)
        if (score >= 60) {
            Hud2.addgold(25);
            table.add(runLabel).expandX().padTop(10);
            ((Game) Gdx.app.getApplicationListener()).setScreen(new com.mygdx.game.Level2.WinScreen2(game));
            stage.addActor(table);






        }

            else if (score <= 59)
                ((Game) Gdx.app.getApplicationListener()).setScreen(new com.mygdx.game.Level2.GameOverScreen2(game));


    }


public static void addscore(int value){
    score += value;
    scoreLabel.setText(String.format("%01d", score));

    prefs.getInteger("score", 0);
    if (score > hScore) {
        prefs.putInteger("highScore", score);
    }
    prefs.flush();
}

public static void addgold(int value){
    gold += value;
    gLabel.setText(String.format("%01d", gold));

    prefs.getInteger("gold", 0);

    prefs.putInteger("goldCoin", gold);
    prefs.flush();
}

The problem seems to be that you have two variables for gold: 问题似乎是您有两个黄金变量:

private static Integer gold;

private static int gCoin;

Your addGold() method updates gold but never gCoin yet your preferences gets the value of gCoin from gCoin = prefs.getInteger("goldCoin", 0); 您的addGold()方法会更新gold但不会更新gCoin,但您的偏好设置会从gCoin = prefs.getInteger("goldCoin", 0);获得gCoin的值gCoin = prefs.getInteger("goldCoin", 0);

gold is static, meaning that instances of Hud2 do not have their own gold variable. gold是静态的,这意味着Hud2实例没有自己的gold变量。 Every time you create an instance of Hud2 , you set gold to 0 because you have written: 每次创建Hud2实例时,您都将gold设置为0因为您已经编写了:

public Hud2(SpriteBatch sb) {
    [...]
    gold = 0;
    [...]
}

I do not know how you have written your code, but it all looks incredibly... fishy. 我不知道您是如何编写代码的,但是看起来都很令人难以置信。 You are mixing static and non-static fields and methods like crazy. 您正在混合使用静态和非静态字段以及疯狂的方法。 To be honest you should probably rewrite the whole class, but if you want a fast fix, my guess is that you can simply remove gold = 0 from the constructor and instead initialize it when you declare it instead. 老实说,您可能应该重写整个类,但是如果您想快速修复,我的猜测是,您可以简单地从构造函数中删除gold = 0 ,而在声明它时对其进行初始化。 Like this: 像这样:

private static Integer gold = 0;

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

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