You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LibGDX技术咨询:地图怪物位置设置及Tiled放置后移动实现

嘿,我来帮你搞定这两个LibGDX的小问题!先解决坐标系错位的坑,再教你怎么移动Tiled里的怪物~

一、让Enemy类的怪物正确显示在地图坐标系

你之前直接用batch.draw(img, position.x, position.y),其实是把怪物画在屏幕坐标系(也就是相机的视口坐标)里,而不是地图的世界坐标系。要让怪物和地图对齐,得把Batch的投影矩阵和相机绑定,这样就能用世界坐标来定位怪物了。

修改你的Enemy类,给它加上相机引用,并且在渲染时同步投影矩阵:

public class Enemy {
    private Texture img;
    private Sprite sprite;
    private SpriteBatch batch;
    private Vector2 position;
    private Camera camera; // 新增相机引用

    public Enemy(Camera camera) {
        this.camera = camera;
        img = new Texture(Gdx.files.internal(path));
        sprite = new Sprite(img);
        batch = new SpriteBatch();
        position = new Vector2();
        // 可以在这里设置怪物的初始世界坐标,比如地图上的(100, 100)
        position.set(100, 100);
        sprite.setPosition(position.x, position.y);
    }

    public void update(float delta) {
        // 同步Batch的投影矩阵到相机,这样画出来的就是世界坐标
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        sprite.draw(batch); // 用Sprite的draw更方便,不用自己管纹理
        batch.end();

        // 比如测试移动:让怪物慢慢向右走
        position.x += 50 * delta;
        sprite.setPosition(position.x, position.y);
    }
}

这样设置后,你给position赋值的就是地图的世界坐标,怪物就会乖乖待在你想要的位置啦~

二、让Tiled里的Bot类怪物能移动

你现在用tiledMapRenderer.renderObject(monster),其实是直接渲染Tiled里的静态对象,没法直接修改它的位置。正确的做法是:从Tiled的对象层里提取怪物的初始位置、纹理信息,然后转换成自己可控制的Sprite,之后就和Enemy类一样自己管理移动和渲染了。

修改你的Bot类,改成这样:

public class Bot {
    private SpriteBatch batch;
    private Sprite botSprite;
    private Vector2 position;
    private Camera camera;

    public Bot(TiledMap tiledMap, String name, Camera camera) {
        this.camera = camera;
        batch = new SpriteBatch();
        position = new Vector2();

        // 从Tiled地图的monster层获取对象
        MapLayer monsterLayer = tiledMap.getLayers().get("monster");
        if (monsterLayer instanceof MapObjectsLayer) {
            MapObjects monsterObjects = ((MapObjectsLayer) monsterLayer).getObjects();
            MapObject monster = monsterObjects.get(name);
            
            // 提取Tiled里怪物的初始位置
            if (monster instanceof TextureMapObject) {
                TextureMapObject textureObj = (TextureMapObject) monster;
                // 获取Tiled里设置的位置
                position.set(textureObj.getX(), textureObj.getY());
                // 用Tiled里的纹理创建Sprite(或者你也可以自己指定纹理)
                botSprite = new Sprite(textureObj.getTextureRegion());
                botSprite.setPosition(position.x, position.y);
            }
        }
    }

    public void update(float delta) {
        // 同样绑定相机投影矩阵
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        if (botSprite != null) {
            botSprite.draw(batch);
            // 测试移动:让怪物向左走
            position.x -= 30 * delta;
            botSprite.setPosition(position.x, position.y);
        }
        batch.end();
    }
}

这样改完,你就可以像控制Enemy一样,通过修改position来移动Bot类的怪物了。核心思路就是不要依赖Tiled的渲染器来画动态对象,而是把Tiled里的对象当成“出生点”,提取信息后自己管理渲染和移动。

另外要注意:记得在游戏的dispose方法里,把Enemy和Bot里的Texture、SpriteBatch都释放掉,避免内存泄漏哦~

内容的提问来源于stack exchange,提问作者RobJan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:36:21