[英]Java LibGDX how to check if sprite stands on specific tile e.g. door?
I want to change the map (method changeMapToWinter()) when sprite stands on specific tile of specific map.当精灵站在特定 map 的特定图块上时,我想更改 map(方法 changeMapToWinter())。 I have no layers on map.
我在 map 上没有图层。
Currently I do something like that if(sprite.getX() > 100 && sprite.getX() < 200 && sprite.getY() > 100 && sprite.getY() < 200) but of course it works only on one map and do not look too good and I guess there is a better solution for that.目前我做了类似的事情 if(sprite.getX() > 100 && sprite.getX() < 200 && sprite.getY() > 100 && sprite.getY() < 200) 但当然它只适用于一个 map 和看起来不太好,我想有一个更好的解决方案。
Texture img;
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
SpriteBatch sb;
Texture texture;
Sprite sprite;
private ShapeRenderer shapeRenderer;
private TiledMapTileLayer tileLayer;
private TiledMap winterMap;
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, w, h);
camera.update();
tiledMap = new TmxMapLoader().load("untitled.tmx");
winterMap = new TmxMapLoader().load("untitled2.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
Gdx.input.setInputProcessor(this);
sb = new SpriteBatch();
texture = new Texture(Gdx.files.internal("bold_brown_blue-1.png"));
sprite = new Sprite(texture);
}
void changeMapToWinter() {
tiledMap.dispose();
winterMap = new TmxMapLoader().load("untitled2.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(winterMap);
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
sb.begin();
sb.draw(sprite, (int) (Gdx.app.getGraphics().getWidth() / 2) - (sprite.getWidth()), (int) (Gdx.app.getGraphics().getHeight() / 2) - (sprite.getHeight() / 2));
sb.end();
Vector3 click = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
TiledMapTileLayer layer = (TiledMapTileLayer)tiledMap.getLayers().get(0);
// WANT TO CHANGE IT
if(sprite.getX() > 100 && sprite.getX() < 200 && sprite.getY() > 100 && sprite.getY() < 200)
changeMapToWinter();
}
Solution you might use can be something like this:您可能使用的解决方案可能是这样的:
Rectangle changePosition = new Rectangle(100,100,100,100);
if(sprite.getBoundingRectangle().overlaps(changePosition)) ...
Constructor for rectangle have this format Rectangle(float x, float y, float width, float height)
.矩形的构造函数具有这种格式
Rectangle(float x, float y, float width, float height)
。
Also this will behave a bit differently from your code as you weren't taking into account size of the sprite, but this code does.此外,这将与您的代码略有不同,因为您没有考虑精灵的大小,但这段代码确实如此。
Btw LibGDX have usefull class for problems like this if you wouldn't be able to find appropriate function in Rectangle class or whatever shape you are using. Btw LibGDX have usefull class for problems like this if you wouldn't be able to find appropriate function in Rectangle class or whatever shape you are using. It is called Intersector .
它被称为Intersector 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.