我正在使用libGDX构建一个简单的自上而下的RPG游戏,但尚未找到有关如何检测平铺地图上碰撞的精确指针。

我有一个看起来像这样的角色类

public class Son{

    SpriteBatch batch;
    Texture texture;
    Sprite sprite;

    private TiledMapTileLayer collisionLayer;


    public Son(float x, float y, TiledMapTileLayer collisionLayer){
        this.collisionLayer = collisionLayer;
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("characters/xter1.png"));
        sprite = new Sprite(texture);
        sprite.setX(x);
        sprite.setY(y);

    }

    public void render(){

        processKeys();
        batch.begin();
        sprite.draw(batch);
        batch.end();

    }


    private void processKeys(){
        if(Gdx.input.isTouched()){
           //Gdx.app.exit();
            Gdx.app.log("x touched", Gdx.input.getX() + "");
            Gdx.app.log("y touched", Gdx.input.getY() + "");

            //480 by 840

            //left
            if(Gdx.input.getX() >= 0 && Gdx.input.getX() <= 80){
                if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/6 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/6 + 80){
                    sprite.setPosition(sprite.getX() - 10, sprite.getY());
                }
            }

            //right
            if(Gdx.input.getX() >= (int)(Gdx.graphics.getWidth()/8.4) && Gdx.input.getX() <= (int)(Gdx.graphics.getWidth()/8.4) + 80){
                if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/6 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/6 + 80) {
                    sprite.setPosition(sprite.getX() + 10, sprite.getY());
                }
            }


            //up
            if(Gdx.input.getX() >= (int)(Gdx.graphics.getWidth()/16.8) && Gdx.input.getX() <= (int)(Gdx.graphics.getWidth()/16.8) + 80){
                if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/3.2 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/3.2 + 80){
                    sprite.setPosition(sprite.getX(), sprite.getY() + 10);
                }
            }
            //down
            if(Gdx.input.getX() >= (int)(Gdx.graphics.getWidth()/16.8) && Gdx.input.getX() <= (int)(Gdx.graphics.getWidth()/16.8) + 80){
                if(Gdx.graphics.getHeight() - Gdx.input.getY() >= Gdx.graphics.getHeight()/48 && Gdx.graphics.getHeight() - Gdx.input.getY() <= Gdx.graphics.getHeight()/48 + 80){
                    sprite.setPosition(sprite.getX(), sprite.getY() - 10);

                }
            }
        }
    }

}


这段代码只是根据用户按下屏幕的哪个部分来更改子画面的位置。

包含平铺地图的Class看起来像这样

public class StageOneScreen implements Screen {
    TiledMap tiledMap;
    OrthographicCamera camera;
    FitViewport viewPort;
    TiledMapRenderer tiledMapRenderer;
    Stage stage;
    ControllerRenderer controllerRenderer;
    Son son;
    private Music music;

    final float GAME_WORLD_HEIGHT = 100;
    final float GAME_WORLD_WIDTH = 50;

    private TiledMap collisionMap;
    private TiledMapTileLayer collisionLayer;


    public StageOneScreen(){
        float aspectRatio = (float) Gdx.graphics.getHeight() / (float) Gdx.graphics.getWidth();

        camera = new OrthographicCamera(GAME_WORLD_HEIGHT * aspectRatio, GAME_WORLD_HEIGHT);
        camera.position.set(GAME_WORLD_WIDTH/2, GAME_WORLD_HEIGHT/2, 0);
        //viewPort = new FitViewport(800, 400, camera);
        camera.setToOrtho(false, 800, 400);
        camera.update();


        tiledMap = new TmxMapLoader().load("stages/stage1.tmx");
        tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);

        stage = new Stage(new ScreenViewport());
        stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void show() {
       // playMusic();
        controllerRenderer = new ControllerRenderer();
        son = new Son(100, 100, collisionLayer);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glBlendFunc(GL20.GL_BLEND_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        tiledMapRenderer.setView(camera);

        tiledMapRenderer.render();
        controllerRenderer.render(camera);
        son.render();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        controllerRenderer.dispose();

    }

    public void playMusic(){
        // load music asset and check if the loop state before playing it
        music =  SoundAsset.loadMusic(SoundAsset.MUSIC_NAME);
        music.setLooping(true);
        if (music.isLooping())
            music.play();
    }
}


在渲染的平铺地图上,我为某些平铺提供了“墙”属性。我也使用两层。我不希望“ Son”角色能够穿过的磁贴在第二层上。如何阻止角色穿过某些区域?

最佳答案

如果您使用的是TiledMap编辑器:http://www.mapeditor.org/
您可以使用编辑器中的一些标记来设置碰撞属性,然后从代码中获取值。

当我使用Libgdx进行小型2D游戏时,我有一种策略模式来绘制和移动对象。
假设您已经设置了一些属性,请阅读它并执行逻辑。类似于“在玩家移动时,如果nextCase == wall,nextCase = currentCase”

关于java - 具有属性和图层的LibGDX平铺 map 碰撞检测,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31779964/

10-13 03:07