如何使用精灵图像作为舞台背景并在舞台背景上添加另一个精灵图像
how to use sprite image as stage background and add another sprite image on the stage background
我在背景中添加了图片,想在背景中添加一些按钮,在下面的代码中,我的图片背景消失了
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();
}
如果你想要一个舞台,那么你需要为扩展 Actor 的精灵创建一个 class。然后您需要将演员添加到舞台上。另一方面,舞台会调用演员批次来绘制精灵。您的代码应该类似于:
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
我在背景中添加了图片,想在背景中添加一些按钮,在下面的代码中,我的图片背景消失了
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();
}
如果你想要一个舞台,那么你需要为扩展 Actor 的精灵创建一个 class。然后您需要将演员添加到舞台上。另一方面,舞台会调用演员批次来绘制精灵。您的代码应该类似于:
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