2016-03-26 6 views
0

私はゲームを持っており、レベルを完了すると25ゴールドを獲得します。私が欲しいのは、スタックして再びプレーして25ゴールドを獲得すると、今では50ゴールドになるということです。私はこれを持っているが、私はスタックすることができない、ゲームは、現在のゲームから25金を覚えている:LibGDXどのようにして既存の値に値を追加できますか?

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

「金」とは何ですか? – Gendarme

+0

ゴールドは0で、レベルを完了すると25ゴールドになります。 – Filip

+0

あなたのコードをさらに表示する必要があります。プログラムの1つのメソッドの背後にあるコードを提供しただけです。クラス全体を表示し、あまりにも多い場合は、不要なメソッドやフィールドをすべて削除します。 – Gendarme

答えて

1

問題は、金のための2つの変数があることのようだ:

private static Integer gold;

private static int gCoin;

あなたaddGold()方法更新goldをまだあなたの好みをgCoinことはありませんgCoinの値をgCoin = prefs.getInteger("goldCoin", 0);

から取得します。
+0

それは私の問題を解決!ありがとうございました :) – Filip

0

goldHud2のインスタンスは、独自のgold変数を持っていないことを意味し、静的である:ここで

は、すべてのコードです。あなたが書かれているので、あなたがHud2のインスタンスを作成するたびに、0goldを設定します。

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

は、私はあなたのコードを書かれているのか分からないが、それはすべての魚...信じられないほどになります。あなたは静かで非静的なフィールドとメソッドを混在させています。正直言ってクラス全体を書き直すべきでしょうが、速い修正が必要な場合は、コンストラクタからgold = 0を単に削除して、代わりに宣言するときに初期化してください。このように:

private static Integer gold = 0;

関連する問題