2014-02-09 22 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