2016-08-03 64 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