您好我是libgdx的新手,并尝试建立一个ludo game.right知道我可以画板但是当我试图显示骰子或任何和平没有任何反应时我附上游戏代码。 请阅读并帮助。
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();
}
答案 0 :(得分:1)
你正在(w/2, h/2)
的位置绘制你的骰子,这超出了你的视口范围。您设置视口,使屏幕右侧的X坐标为0.5,但您的精灵左侧为x=w/2
,其中w是窗口宽度的像素数。
即使您将相机尺寸设置为与窗口((w, h)
)匹配,也会出现问题,因为您要绘制精灵,使其左边缘与屏幕右边缘匹配。默认情况下,裸片精灵的原点将是它的左下角。
如果您不打算放大和缩小场景,可以使用camera = new OrthographicCamera(w, h);
将相机视口设置为窗口大小更简单然后您就会知道您的单位相当于像素。