LibGDX如何为现有值增加价值?

时间:2016-03-26 00:24:29

标签: java android libgdx

我有一个游戏,当你完成一个关卡时,你可以获得25金币。我想要的是它可以堆叠,所以如果我再次玩并获得25金币,我现在将获得50金币。我有这个,但我无法叠加,游戏只记得当前游戏中的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();
}

我想我应该写一些类似的东西:

goldCoin + gold

goldCoin是我总共获得的,而黄金是我从当前游戏中得到的。

以下是所有代码:

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();
}

2 个答案:

答案 0 :(得分:1)

问题似乎是你有两个黄金变量:

class EventAdmin(admin.ModelAdmin): filter_horizontal = ('participantes',)

private static Integer gold;

您的private static int gCoin;方法更新addGold()但从未gCoin,您的偏好设置从gold获得gCoin的值

答案 1 :(得分:0)

gold是静态的,这意味着Hud2的实例没有自己的gold变量。每次创建Hud2的实例时,都会将gold设置为0,因为您已经写过:

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

我不知道你是如何编写代码的,但它看起来都令人难以置信......很可疑。您正在混合静态和非静态字段和方法,如疯狂。说实话,你应该重写整个类,但如果你想快速修复,我的猜测是你可以简单地从构造函数中删除gold = 0,而是在声明它时初始化它。像这样:

private static Integer gold = 0;