java 带有scene2d和actor的libgdx没有显示精灵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14969507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
libgdx with scene2d and actor is not displaying the sprite
提问by Roar Skullestad
I am testing out Libgdx and Scene2d. I expected this small program to display a logo, but it draws a black screen only. Any idea what am I missing?
我正在测试 Libgdx 和 Scene2d。我希望这个小程序显示一个标志,但它只画了一个黑屏。知道我错过了什么吗?
public class MyGame implements ApplicationListener {
private Stage stage;
@Override
public void create() {
stage = new Stage(800, 800, false);
Gdx.input.setInputProcessor(stage);
MyActor actor = new MyActor();
stage.addActor(actor);
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void dispose() {
stage.dispose();
}
@Override
public void resize(int width, int height) {
stage.setViewport(800, 800, false);
}
}
public class MyActor extends Actor {
Sprite sprite;
public MyActor() {
sprite = new Sprite();
sprite.setTexture(new Texture("data/libgdx.png"));
setWidth(sprite.getWidth());
setHeight(sprite.getHeight());
setBounds(0, 0, getWidth(), getHeight());
setTouchable(Touchable.enabled);
setX(0);
setY(0);
}
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
batch.draw(sprite, getX(), getY());
}
}
回答by itamarb
Construct the sprite with the texture and use Gdx.file.internal:
使用纹理构造精灵并使用 Gdx.file.internal:
sprite = new Sprite(new Texture(Gdx.files.internal("data/libgdx.png")));
Anyway, if you just want to display and act on images, you might prefer to use Image class:
无论如何,如果您只想显示和操作图像,您可能更喜欢使用 Image 类:
private Stage stage;
private Texture texture;
@Override
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
com.badlogic.gdx.scenes.scene2d.ui.Image actor = new com.badlogic.gdx.scenes.scene2d.ui.Image(region);
stage.addActor(actor);
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
回答by Raf
I was getting a black screen too until I explicitly set Actor
's height (setHeight(height)
) and width (setWidth(width)
) to the Sprite
's values.
在我将Actor
的高度 ( setHeight(height)
) 和宽度 ( setWidth(width)
)明确设置为Sprite
的值之前,我也遇到了黑屏。
回答by David Martin
tex = new Texture(Gdx.files.internal("happy.png"));
Image happy = new Image(tex);
/* happy.setBounds(happy.getX(), happy.getY(), happy.getWidth(), happy.getHeight()); not needed if using full image */
stage.addActor(happy);
回答by avanderw
Your problem is most likely this line, in the draw method
你的问题很可能是这一行,在 draw 方法中
batch.draw(sprite, getX(), getY());
The code I have seen when drawing sprites is
我在绘制精灵时看到的代码是
sprite.draw(batch);