Java 在平铺贴图libgdx上显示精灵
我正在用LibGDX创建一个游戏。我已经采取了在像素地下城使用的瓷砖,并创建了一个瓷砖地图使用 主要角色的类是Java 在平铺贴图libgdx上显示精灵,java,libgdx,Java,Libgdx,我正在用LibGDX创建一个游戏。我已经采取了在像素地下城使用的瓷砖,并创建了一个瓷砖地图使用 主要角色的类是Actor的子类,因为角色是动画的,所以我使用以下代码绘制精灵: if (direction.isEastwards() || direction.isNorthwards()) { Vector3 projectedPosition = getLocation().getCamera().project( new Vector3(16 *
Actor
的子类,因为角色是动画的,所以我使用以下代码绘制精灵:
if (direction.isEastwards() || direction.isNorthwards()) {
Vector3 projectedPosition = getLocation().getCamera().project(
new Vector3(16 * getX() + 1, Gdx.graphics.getHeight()
- (16 * getY()) - 15, 0));
batch.draw(current.getKeyFrame(
animTime += Gdx.graphics.getDeltaTime(), true),
projectedPosition.x, projectedPosition.y);
} else {
Vector3 projectedPosition = getLocation().getCamera().project(
new Vector3(16 * getX() + 13, Gdx.graphics.getHeight()
- (16 * getY()) - 15, 0));
batch.draw(current.getKeyFrame(
animTime += Gdx.graphics.getDeltaTime(), true),
projectedPosition.x, projectedPosition.y);
}
当我在Eclipse中启动游戏时,精灵最初出现在正确的位置。但是,如果我调整屏幕大小,精灵将不再位于正确的位置,最终将从地图上消失
在我开始使用投影之前也发生了同样的事情,我认为问题与投影有关。因为这是一个我从未探索过的领域,所以我在一个小时后决定能够解决这个问题并寻求帮助
动画翻转取决于角色面对的方向,这就是if/else子句存在的原因
附录:该阶段是使用新阶段(新的ExtendViewport(800600),批次)创建的代码>,调整大小方法更新相机,批次设置为投影矩阵
以下是更相关的代码:
摄像头和地图初始化:
camera=new OrthographicCamera();
camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
mapRenderer=new OrthogonalTiledMapRenderer(map,batch);
渲染方法:
camera.update();
batch.setProjectionMatrix(camera.combined);
mapRenderer.setView(camera);
mapRenderer.render();
stage.act();
stage.draw();
调整大小方法:
camera.viewportWidth=width;
camera.viewportHeight=height;
camera.update();
演员画法:
@Override
public void draw(Batch batch, float parentAlpha) {
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
if (direction.isEastwards() || direction.isNorthwards()) {
Vector3 projectedPosition = getLocation().getCamera().project(
new Vector3(16 * getX() + 1, Gdx.graphics.getHeight()
- (16 * getY()) - 15, 0));
batch.draw(current.getKeyFrame(
animTime += Gdx.graphics.getDeltaTime(), true),
projectedPosition.x, projectedPosition.y);
} else {
Vector3 projectedPosition = getLocation().getCamera().project(
new Vector3(16 * getX() + 13, Gdx.graphics.getHeight()
- (16 * getY()) - 15, 0));
batch.draw(current.getKeyFrame(
animTime += Gdx.graphics.getDeltaTime(), true),
projectedPosition.x, projectedPosition.y);
}
// Walk animation displays for 0.2 seconds
if (current == walk && animTime >= 0.2) {
current = idle;
animTime = 0;
}
}
我认为这个问题是因为你没有把精灵和相机投影结合起来。如下所示设置spriteBatch:
spriteBatch.setProjectionMatrix(camera.combined);
并且不要错过“调整大小”方法中的“更新视口”:
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
有了它,你可以调整或缩放zoomout,spriteBatch将缩放到相机投影。我肯定添加了batch.setProjectionMatrix(camera.combined)代码>。我会检查我是否忘记了更新行。更新了我的问题;添加了更多相关代码。是否也在“调整大小”中应用视口更新?类似这样的内容:stage.getViewport().update(Gdx.graphics.getWidth(),Gdx.graphics.getHeight,true);或者任何你想要的屏幕大小?是的stage.getViewport().update(宽度、高度、真值)事实上,我想我知道问题所在。角色正在正确缩放。地图不是。