2016-06-11 53 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

考虑将此问题移至[游戏开发](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")); 

loading10loading10Tableloading90loading90Table然后加mainTableloading10Table到舞台。在更新方法中,假设在数组中插入了“loading10Table”:

stage.getActors()[1] = loading90Table;