2016-06-11 17 views
1

スコア、時間、画像の読み込みを表示するHUDを描画しようとしていますが、スコアが高くなると画像を更新して変更したいのですが、働いていない。私は、更新メソッドで新しいテーブルを定義し、毎回異なるイメージを置くことができると思うかもしれませんが、毎回新しいテーブルを定義したくありません。唯一のテーブル内の画像を更新するために、とにかくそこにある: これはコードです:Libgdxは動的にテーブル内の画像を変更します

public class Hud implements Disposable{ 

    public Stage stage; 
    private Viewport viewport; 
    private static Integer worldTimer; 
    private float timecount; 
    private static Integer score; 
    Label countdownLabel; 
    private static Label scorelabel; 
    private Label timelabel; 
    private Label levellabel; 
    private Label worldlabel; 
    private Label mariolabel; 
    private Image loading; 
    private Table table; 
    public Hud(SpriteBatch sb) 
    { 
     worldTimer=40; 
     timecount=0; 
     score=0; 
     viewport=new FitViewport(Fruits.V_WIDTH,Fruits.V_HIEGT,new OrthographicCamera());//2 
     stage=new Stage(viewport,sb);//stage is as box and try to put widget and organize things inside that table 
     table = new Table(); 
     table.top();//table at top of our stage 
     table.setFillParent(true);//table is now fill all the stage 
     countdownLabel=new Label(String.format("%02d",worldTimer),new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx 
     scorelabel=new Label(String.format("%02d",score),new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx 
     timelabel=new Label("TIME",new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx 
     levellabel=new Label("1-1",new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx 
     worldlabel=new Label("WORLD",new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx 
     mariolabel=new Label("Score" ,new Label.LabelStyle(new BitmapFont(), Color.BLACK));//label for gdx 
     loading = new Image(new Texture("10%.png")); 
     //loading.setSize(40, 10); 
     table.add(mariolabel).expandX().padTop(10); 

     table.add(timelabel).expandX().padTop(10); 
     table.add(worldlabel).expandX().padTop(10); 
     table.row(); 
     table.add(scorelabel).expandX(); 

     table.add(countdownLabel).expandX(); 
     table.add(loading).width(50).height(10); 

     stage.addActor(table); 

    } 
    public void update(float dt) 
    { 
     timecount+=dt; 
     if (timecount>1) 
     { 
      worldTimer--; 
      if(worldTimer>=0) { 
       countdownLabel.setText(String.format("%02d", worldTimer)); 
       loading=new Image(new Texture("90.png")); 
      } 
      timecount=0; 

     } 
    } 
    public static int getTime() 
    { 

     return worldTimer; 
    } 

    public static void addScore(int value) 
    { 
     score +=value; 
     scorelabel.setText(String.format("%02d",score)); 
    } 
    public static int getScore() 
    { 
     return score; 
    } 
    @Override 
    public void dispose() { 
     stage.dispose(); 
    } 
} 
+0

この質問を[game development](http://gamedev.stackexchange.com/)サイトに移動することを検討してください。 –

+0

オブジェクトと参照の違いを理解するには、Javaの基本について少しお読みください。参照を再割り当てすると、以前に参照されたオブジェクトに何も起きることはありませんが、おそらくガベージコレクションのために利用できるようになります。 – Tenfour04

+0

ここで新しいテーブルを作成せずにテーブルにオブジェクトを追加する方法 テーブルを作成してメソッド内でオブジェクトを割り当て、更新メソッドで呼び出すと、多くのテーブルが作成される –

答えて

0

あなたは、実行時にその場でそれらをロードするのではなく、あなたのコンストラクタ内のすべてのテクスチャをロードする必要があります。あなたがやろうとしているが、容易にCで達成されます++が、それはあなたが例えばを更新する必要がある唯一のものですので、Javaで、それを行うための一つの方法は、テクスチャのテーブルの列を作成するために、次のようになります。

private Table maintable = new Table(); 
private Table loading10Table = new Table(); 
private Table loading90Table = new Table(); 
private Image loading10 = new Image(new Texture("10%.png")); 
private Image loading90 = new Image(new Texture("90%.png")); 

loading10からloading10Tableおよびloading90からloading90Tableを追加し、mainTableおよびloading10Tableをステージに追加します。更新メソッドでは、「loading10Table」が配列の2番目に挿入されたと仮定します。

stage.getActors()[1] = loading90Table; 
関連する問題