2014-02-09 31 views
1

試圖製作一個簡單的答題器,顯示隨每次點擊/點擊而增加的數字。 該計數器工作正常,但我不能讓它顯示一個數字,每次點擊更新。 System.out.print打印計數器正在工作,但font.draw不會隨每個水龍頭更新。不知道現在該做什麼。如何使String.draw在字符串更新時更新[LibGDX]

package com.me.mygdxgame; 

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Input; 
import com.badlogic.gdx.Input.Keys; 
import com.badlogic.gdx.InputProcessor; 
import com.badlogic.gdx.graphics.Color; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.BitmapFont; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.math.Vector2; 

public class MyGdxGame implements ApplicationListener { 

    SpriteBatch batch; 
    BitmapFont font; 
    Texture mario; 
    int cash = 0; 
    String money = String.valueOf(cash); 
    Vector2 position; 

    public boolean touchDown(int x, int y, int pointer, int button) { 
     if (button == Input.Buttons.LEFT) { 
      System.out.println("Test"); 
      return true; 
     } 
     return true; 
    } 

    @Override 
    public void create() { 

     mario = new Texture(Gdx.files.internal("data/mario.jpeg")); 
     batch = new SpriteBatch(); 
     font = new BitmapFont(); 
     position = new Vector2(150, 150); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     font.dispose(); 
    } 

    @Override 
    public void render() { 
     if(Gdx.input.justTouched()){ 
      cash++; 
      System.out.println("e"); 
      System.out.println(cash); 
     } 


     Gdx.gl.glClearColor(1, 1, 1, 1); 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

     batch.begin(); 
     font.draw(batch, money, 300, 260); 
     batch.end(); 

    } 

    @Override 
    public void resize(int width, int height) { 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 


} 
+1

現金!=錢... –

+0

哼,從來不得不用「String money = String.valueOf(cash);」不知道它是如何工作的。你會如何建議讓「現金」成爲可打印的字符串? – CodingNub

回答

3

你增加cash,但此後再也沒有新的值賦給money。最簡單的解決方案是在增加cash後添加一行money = Integer.toString(cash);money = String.valueOf(cash);

+0

我已經這樣做了,但現在'String money = String.valueOf(cash); '給出的錯誤「令牌上的語法錯誤」;「,,expected」當我刪除「;」 'money = Integer.toString(現金); '給出錯誤:令牌「金錢」上的語法錯誤,刪除此令牌 – CodingNub

+0

您必須做** money = String.valueOf(現金); **每當您更改其值時。 – Lestat