【问题标题】:Paste texture as background libgdx将纹理粘贴为背景 libgdx
【发布时间】:2014-12-09 12:27:39
【问题描述】:

我正在尝试将 png 文件粘贴到屏幕上作为我的游戏的背景,到目前为止,我似乎无法让它工作,我也不太确定。

我创建了一个地图类并让它像这样创建纹理:

public static Texture backgroundTexture;
public static Sprite backgroundSprite;


public Maps(){
    try{
        backgroundTexture = new Texture(Gdx.files.internal("background.png"));
        backgroundSprite = new Sprite(backgroundTexture);
    }catch (Exception e){
        e.printStackTrace();
    }
}

public void renderBackground(SpriteBatch batch){
    batch.draw(backgroundTexture, 1000, 800);
}

然后在我的主类中,我在 create 方法中这样调用它:

batch = new SpriteBatch();
map = new Maps();
    batch.begin();
    map.renderBackground(batch);
    batch.end();

但这不起作用?它应该在渲染方法中因为它需要每回合刷新背景吗?我已经在渲染中尝试过,但仍然无法正常工作。我的屏幕尺寸是 WXH 1000x800

【问题讨论】:

  • 这一行的 (batch.draw(backgroundTexture, 1000, 800);) X 和 Y 参数是纹理左下角的位置。我想你要么想使用 (0, 0) 要么 (-screenWidth/2, -screenHeight/2),这取决于你的相机是如何设置的。
  • 您的精灵批次没有使用相机吗?如果不是,它会将屏幕尺寸视为 2 x 2。
  • @noone 这是 OpenGL 的默认设置。如果您不在顶点着色器中变换顶点位置(或者如果变换矩阵与 SpriteBatch 中的相同,如果保持不变),则所有内容都将映射到 (-1, -1) 到 (1, 1)。

标签: java opengl libgdx 2d


【解决方案1】:

试试这个:

OrthographicCamera cam = new OrthographicCamera(1000, 800);
SpriteBatch batch = new SpriteBatch();
Maps map = new Maps();

在您的渲染/更新方法中:

batch.setProjectionMatrix(cam.combined);
batch.begin();
map.renderBackground(batch);
batch.end();

在您的地图渲染方法中:

batch.draw(backgroundTexture, 0, 0, 1000, 800); //x, y, width, height

【讨论】:

  • 这很好,但我再也看不到我所有的图形和东西了。现在是贴在上面了吗?固定,是因为我应该在绘制玩家之前调用它!这行得通!谢谢大家!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多