我对编程很陌生,所以请耐心等待...
我正在制作一个 2d 基本游戏,只是为了在 android 工作室练习编程,无法将我的精灵放在屏幕上的正确位置。另外,当我绘制精灵时,它看起来被拉伸并且质量很差。任何帮助不胜感激!
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture background;
Texture ball;
@Override
public void create () {
batch = new SpriteBatch();
background = new Texture("gamebackground.png");
ball = new Texture("ball2.png");
}
@Override
public void render () {
batch.begin();
batch.draw(background, 0,0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.draw(ball, 0,0, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
batch.end();
}
您需要保持原始的宽/高比:
与其将其缩放到屏幕大小的一半,不如像这样定义缩放:
float scaleFactor = 2.0f;
batch.draw(ball, 0,0, ball.getWidth()*scaleFactor, ball.getHeight*scaleFactor);
如果你的图像是"模糊的",并且你希望单个像素保持清晰,请尝试像这样加载纹理:
ball = new Texture("ball2.png");
ball.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
这可以防止在缩放纹理时(默认)线性插值。