[英]display sprite on tile map libgdx
I am creating a game in LibGDX. 我正在LibGDX中创建游戏。 I have taken the tiles used in Pixel Dungeon and created a tile map using Tiled . 我已经获取了Pixel Dungeon中使用的图块,并使用Tiled创建了图块地图。
The main character's class is a subclass of Actor
, and because the character is animated, I am using this code to draw the sprite: 主要角色的类是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);
}
When I launch the game in Eclipse, the sprite initially appears in the correct location. 当我在Eclipse中启动游戏时,精灵最初会显示在正确的位置。 However, if I resize the screen, the sprite ceases to be in the correct location and eventually will disappear off the map. 但是,如果我调整屏幕大小,则精灵将不再位于正确的位置,最终将消失在地图上。
The same thing happened before I started using projection, and I figured the problem was related to projection. 在开始使用投影之前,发生了同样的事情,并且我发现问题与投影有关。 As this is an area I have not explored before I decided after an hour of being able to solve this to ask for help. 由于这是我尚未探索的领域,因此在决定一个小时后才能够解决此问题,以寻求帮助。
The animation flips depending on which direction the character faces, which is why the if/else clause is there. 动画根据角色面向的方向翻转,这就是为什么if / else子句存在的原因。
Addendum: The stage is created with new Stage(new ExtendViewport(800,600),batch);
附录:使用new Stage(new ExtendViewport(800,600),batch);
创建new Stage(new ExtendViewport(800,600),batch);
, the resize method updates the camera, and the batch is set to the projection matrix. ,则resize方法将更新摄影机,并将批次设置为投影矩阵。
Here is more relevant code: 这是更相关的代码:
Camera and map initialisation: 相机和地图初始化:
camera=new OrthographicCamera();
camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
mapRenderer=new OrthogonalTiledMapRenderer(map,batch);
Render method: 渲染方法:
camera.update();
batch.setProjectionMatrix(camera.combined);
mapRenderer.setView(camera);
mapRenderer.render();
stage.act();
stage.draw();
Resize method: 调整大小方法:
camera.viewportWidth=width;
camera.viewportHeight=height;
camera.update();
Actor draw method: 演员画法:
@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;
}
}
I think the problem it caused because you dont combine the sprite with camera projection. 我认为它引起的问题是因为您没有将精灵与相机投影结合在一起。 set your spriteBatch like this : 像这样设置您的spriteBatch:
spriteBatch.setProjectionMatrix(camera.combined);
And dont miss update viewport in resize method : 并且不要错过使用调整大小方法的更新视口:
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
With that, you can resize or zoomin zoomout, the spriteBatch it scalled to the camera projection. 这样,您可以调整大小或放大缩小,将其调用的spriteBatch投影到相机投影。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.