2016-06-11 5 views
1

점수와 시간 및 이미지를로드하는 것처럼 보이지만 점수가 높을 때 이미지를 업데이트하고 변경하려고하는 HUD를 그리려는 것입니다. 작동 안함. 아마 나는 update 메소드에서 새로운 테이블을 정의하고 매번 다른 이미지를 넣을 수 있다고 생각했지만 매번 새로운 테이블을 정의하고 싶지는 않습니다. 단지 표 안에 이미지를 업데이트 할 수 어쨌든이 : 이 코드입니다 :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에서 달성 될 ++하지만 당신이 업데이트해야 할 유일한 일이기 때문에 자바에서, 한 가지 방법은 텍스처에 대한 테이블의 배열을 생성하는 것입니다 할 예 :

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-loading10Tableloading90-loading90Table을 추가 한 다음 mainTableloading10Table을 스테이지에 추가하십시오. 업데이트 방법에서 "loading10Table"이 배열에 두 번째로 삽입되었다고 가정합니다.

stage.getActors()[1] = loading90Table; 
관련 문제