Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

android - 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. I have no layers on 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.

    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();
    }
question from:https://stackoverflow.com/questions/65864642/java-libgdx-how-to-check-if-sprite-stands-on-specific-tile-e-g-door

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...