簡體   English   中英

LibGDX TiledMap-不檢測碰撞

[英]LibGDX TiledMap - don't detect Collisions

首先,對不起,因為我的英語不好。 自從我學德語以來,我就忘記了英語。

我正在使用libGDX進行測試,我的代碼未檢測到任何沖突。

在我的屏幕上:

    public Pantalla(SpriteBatch batch_1) {
        batch = batch_1;
        screenWidth = Gdx.graphics.getWidth();
        screenHeight = Gdx.graphics.getHeight();

        stage = new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()),batch);
        Gdx.input.setInputProcessor(stage);

        camera = new OrthographicCamera();
        camera.setToOrtho(false, 1000, 840);
        camera.update();

        mapas =  new Mapas(camera);

        //              ACTORS 
        indiana_actor = new indiana_Actor();

    //Here comes TOUCHPAD with Skin blaBlaBla...
     if (touchpad.isTouched()) {  
              if (touchpad.getKnobX() > 120) {
                            indiana_actor.moveBy(32,0);
                            camera.translate(32, 0);
                            return; }
}

stage.addActor(indiana_actor);
            stage.addActor(touchpad);
            Gdx.input.setInputProcessor(stage);
}



    public void render(float delta) {//TODO RENDER
         Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


            mapas.MapasRender(camera,indiana_actor);
            batch.begin();
        batch.end();

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

    }

indiana_actor類別:

 public indiana_Actor() {
        W=Gdx.graphics.getWidth(); H=Gdx.graphics.getHeight();
        bounds=new Rectangle((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());

    }
    Animation anim_temp;


    @Override
    public void draw(Batch batch, float parentAlpha) {
        stateTime += Gdx.graphics.getDeltaTime();
        batch.setColor(getColor());
batch.draw(Assets.indiana_stop_arriba, (W/2), (H/2), 110, 160);

        bounds=new Rectangle((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());

    }

和Mapas類。 在這些類中,我在“ objetos” TiledLayer中獲得對象,並嘗試在渲染器中使用for(...)檢查沖突。

public Mapas(OrthographicCamera camera2){
    map = new TmxMapLoader().load("terrain/prueba.tmx");
    renderer = new OrthogonalTiledMapRenderer(map, 10);//ESCALA
    renderer.setView(camera2);

    collisionObjects = map.getLayers().get("objetos").getObjects();
    collisionRects = new Array<Rectangle>();
    collisionRects_total=collisionObjects.getCount();
    int tileWidth = 32; // whatever your tile width is
    int tileHeight = 32; // whatever your tile height is
    for (int i = 0; i < collisionObjects.getCount(); i++) {
        RectangleMapObject obj = (RectangleMapObject) collisionObjects.get(i);
        Rectangle rect = obj.getRectangle();
        collisionRects.add(new Rectangle(rect.x / tileWidth, rect.y / tileHeight, rect.width / tileWidth, rect.height / tileHeight));
    }


}
public void MapasRender(OrthographicCamera camera2,indiana_Actor indi){
    camera2.update();
    renderer.setView(camera2);
    renderer.render();

    for (int i = 0; i < collisionRects_total; i++) {
        Rectangle rect = collisionRects.get(i);
        if (indi.bounds.overlaps(rect)){
            Gdx.app.log("EVENTO", "MAPAS RENDER - COLISION!");
        }if (rect.overlaps(indi.bounds)){
            Gdx.app.log("EVENTO", "MAPAS RENDER - COLISION!");
        }
    }
} 

我知道(通過logcat)“ for(int i = 0; i <crashRects_total; i ++){Rectangle rect = crashRects.get(i);“總是得到objectsRectangle,但是在下一行中發現任何重疊。

那是actor bounds-rectangle的問題嗎?在通過地圖移動actor時有問題嗎?

提前致謝!!

為什么不首先檢查您的碰撞矩形是否正確繪制在位置上。 您可以致電

  shapeRenderer.begin(ShapeType.Line);
  shapeRenderer.setColor(Color.NAVY);

  shapeRenderer.rect(object.getrect().x,
 object.getrect().y,object.getrect().width,
  object.getrect().height);

可能是您在錯誤的位置繪制了碰撞矩形,因此永遠不會發生碰撞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM