2016-08-03 65 views
0

我想做一個擴展Actor類的對象。但每當我調用Stage的draw()方法時,它只會繪製我添加到它的按鈕。這裏是我的代碼,也許有人可以幫助我?如何擴展Actor並在舞臺上繪製它? Libgdx

代碼爲我的Screen類(假設我已經覆蓋所有必要的方法和我進口的所有必要的類): 公共類播放屏幕實現屏幕{

SpriteBatch batch; 
Game game; 
Table table; 
Skin skin; 
Stage stage; 
BitmapFont font; 
TextButton button; 
TextDisplay display; 


public PlayScreen(MyGdxGame game, SpriteBatch batch){ 

    this.game = game; 
    this.batch = batch; 

    //instantiating Stage, Table, BitmapFont, and Skin objects. 
    font = new BitmapFont(Gdx.files.internal("MyTruefont.fnt")); 
    table = new Table(); 
    stage = new Stage(); 
    skin = new Skin(Gdx.files.internal("Test.json")); 


    button = new TextButton("Start!", skin, "default"); 
    display = new TextDisplay(font, this); 

    //Setting table properties 
    table.setWidth(stage.getWidth()); 
    table.align(Align.center|Align.top); 
    table.setPosition(0, Gdx.graphics.getHeight()); 
    table.padTop(30); 

    //Adding actors to table 
    table.add(button).padBottom(50); 
    table.add(display); 

    //Adding table to stage and setting stage as the input processor 
    stage.addActor(table); 
    Gdx.input.setInputProcessor(stage); 
} 



@Override 
public void show() { 

} 

@Override 
public void render(float delta) { 

    Gdx.gl.glClearColor(1, 0, 0, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    batch.begin(); 
    stage.draw(); 
    batch.end(); 
} 

這裏是擴展演員我textDisplay形式類(再次假設所有其他方法已被重寫,所有必要的類已導入):

public class TextDisplay extends Actor implements Drawable { 

    Texture texture; 
    BitmapFont font; 
    String str; 

    public TextDisplay(BitmapFont font, Screen screen){ 
     this.font = font; 

     texture = new Texture(Gdx.files.internal("TextArea.png")); 

     str = ""; 

     setLeftWidth(texture.getWidth()); 
     setRightWidth(texture.getWidth()); 

     setBottomHeight(texture.getHeight()); 
     setTopHeight(texture.getHeight()); 
    } 



@Override 
public void draw(Batch batch, float x, float y, float width, float height) { 
    batch.draw(texture, x, y); 
} 

回答

1

你推翻了錯誤draw方法。演員的抽獎方式簽名是:

public void draw (Batch batch, float parentAlpha) 

你超越了Drawable中的一個。不知道你爲什麼要實現Drawable。已經有一個Actor可用於繪製文本,稱爲標籤。另外,如果你想把你的Actor放在一個表中,你需要設置它的寬度和高度,或者它在表格中的單元格的寬度和高度。

+0

哦,好的謝謝。我實現了drawable來查看它是否可以解決問題。我想我可以擺脫這一點。我仍然能夠畫出想要代表我的演員的形象嗎?或者錯過了什麼? –

+0

此外,標籤類不會做我想要的。 –

+0

你可以在'draw'方法中繪製任何你想要的東西。 – Tenfour04