我将图像添加到背景中,并希望在背景中添加一些按钮,在我的图像背景下面的代码中消失
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
spriteBatch.begin();
spriteBatch.setProjectionMatrix(camera.combined);
sprite.draw(spriteBatch);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
spriteBatch.end();
}
按钮图像消失,修改上面的代码
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
spriteBatch.begin();
spriteBatch.setProjectionMatrix(camera.combined);
sprite.draw(spriteBatch);
spriteBatch.end();
}
答案 0 :(得分:1)
如果你想要一个舞台,那么你需要为扩展Actor的精灵创建一个类。然后你需要将演员添加到舞台上。另一方面,舞台将调用actor批处理来绘制精灵。你的代码应该是这样的:
public class MyActor extends Actor {
Sprite sprite;
public MyActor () {
sprite = new Sprite..
//sprite features
...
}
@Override
public void act(float delta) {
super.act(delta);
}
@Override
public void draw (Batch batch, float parentAlpha) {
sprite.draw(batch);
}
}
然后当你创建舞台
//this will create your sprite
MyActor actor = new MyActor();
stage.addActor(actor);
并且在舞台渲染方法中只留下:
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw(); //this will call the batch to draw the sprite
}
有关详情,请查看scene2d docs
此外,有关完整示例,请查看here