我的精灵在libgdx中不显示

我的精灵在libgdx中不显示,libgdx,sprite,Libgdx,Sprite,您好,我是libgdx新手,尝试创建一个ludo游戏。我知道我可以画棋盘,但当我尝试显示骰子或任何和平时,我会附加游戏代码。 请阅读和帮助 public void create() { float w = Gdx.graphics.getWidth(); float h = Gdx.graphics.getHeight(); batch = new SpriteBatch(); camera = new OrthographicCamera(1, h / w);

您好,我是libgdx新手,尝试创建一个ludo游戏。我知道我可以画棋盘,但当我尝试显示骰子或任何和平时,我会附加游戏代码。 请阅读和帮助

public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    batch = new SpriteBatch();
    camera = new OrthographicCamera(1, h / w);
    boardtexture = new Texture(Gdx.files.internal("data/ludo-boardx2.png"));
    boardtexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    boardsprite = new Sprite(boardtexture);
    boardsprite.setSize(1f,
            1f * boardsprite.getHeight() / boardsprite.getWidth());
    boardsprite.setOrigin(boardsprite.getWidth() / 2,
            boardsprite.getHeight() / 2);
    boardsprite.setPosition(-boardsprite.getWidth() / 2,
            -boardsprite.getHeight() / 2);
    Texture dice = new Texture(Gdx.files.internal("data/dices.png"));
    dice.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    dtemp = new Sprite(dice);
    dtemp.setSize(1f, 1f * dtemp.getHeight() / dtemp.getWidth());
            dtemp.setPosition(0f, 0f);


    // System.out.println(w/2+" " +h/2);
    // System.out.println(dtemp.getWidth()+":"+dtemp.getHeight());
    // animatedDice.setPosition(new Vector2(0, 0));
    System.out.println(animatedDice.getX() + ":" + animatedDice.getY()
            + "\n" + w + ":" + h);

}
  public void render() {
  Gdx.gl.glClearColor(1, 1, 1, 1);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

  batch.setProjectionMatrix(camera.combined);
  // camera.update();
  handleInput();
  batch.begin();
  boardsprite.draw(batch);
  dtemp.draw(batch);
  //animatedDice.draw(batch);
  batch.end();
  // animatedDice.update();
  }

您在
(w/2,h/2)
位置绘制模具,这超出了视口的范围。您可以设置视口,使屏幕右侧的X坐标为0.5,但精灵的左侧位于
X=w/2
,其中w是窗口宽度的像素数

即使您已将相机大小设置为与窗口匹配(
(w,h)
),此处也会出现问题,因为您将绘制精灵,使其左边缘与屏幕右边缘匹配。默认情况下,模具精灵的原点将位于其左下角


如果不打算放大和缩小场景,则使用
camera=new orthographic camera(w,h)将摄影机视口设置为窗口大小可能会更简单然后你就会知道你的单位相当于像素。

你能解释更多关于在这种状态下设置视口的内容吗?我的意思是,根据你的计算,你说屏幕右侧的X坐标是0.5。我需要更多的解释才能理解。构造器
新的正交摄影机(宽度、高度)
定义了游戏世界的单位。无论你在那里输入什么数字,都会设置单位的刻度。宽度是您的视口宽度的单位数,因此,如果像您那样将其设置为
1
,则宽度为1的精灵将与屏幕的宽度完全相同。屏幕是一个单位宽,中心在(0,0),因此屏幕的右边缘与X坐标0.5对齐。我尝试在(0,0)中绘制骰子,以显示在屏幕中心,没有机会。你能告诉我在屏幕中央要画的骰子的正确位置吗没有骰子,嗯?:)您是否将骰子精灵的大小设置为小于摄影机视口宽度?如果骰子精灵大约有50个单位宽,而屏幕只有一个单位宽,你只能看到它的左下角的像素,这可能是一个透明的像素。我根据我们的对话更新了我的代码,你看不到吗