简体   繁体   English

Java LibGDX 如何检查精灵是否站在特定的瓷砖上,例如门?

[英]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.

相关问题 如何在java中重新绘制特定区域(例如圆圈)? - How to repaint a specific area (e.g. circle) in java? 如何检查for循环中当前索引处的字符是否为特定字符(例如字母A,数字2等)? - How to check if character at current index in for loop is a specific character (e.g. the letter A, the number 2, etc)? 一个Java线程如何检查另一个Java线程的状态,例如另一个是否被阻止? - How can one Java thread check the state of another one, e.g. whether the other one is blocked? 如何告诉Java将逻辑字体(例如SansSerif)映射到系统上的特定字体? - How do I tell Java to map a logical font (e.g. SansSerif) to a specific font on my system? Java Path 如何转换为例如 InputStream - How is Java Path converted to e.g. InputStream 在图块地图libgdx上显示精灵 - display sprite on tile map libgdx 有没有办法检查Web控制台(例如FireBug)是否以java语言打开? - Is there a way to check if web console (e.g. FireBug) is open in java language? 等待/睡眠直到 java 中的特定时间(例如星期四 10:59) - Wait/sleep until a specific time (e.g. Thursday at 10:59) in java 使用Java检查Git存储库是私有的还是公共的(例如GitHub) - Check if Git repository is private or public (e.g. for GitHub) using Java Java替换String中的每个第N个特定字符(例如空格) - Java replace every Nth specific character (e.g. space) in String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM