0

首先,对不起,因为我生疏的英语。因为我学了德语,所以我忘记了英语。

我正在使用 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();

    }

印第安纳演员类:

 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 < collisionRects_total; i++) { Rectangle rect = collisionRects.get(i);” 总是得到 objectsRectangle ,但在下一行找到任何重叠。

这是演员边界矩形的问题吗?在地图中移动演员时有问题吗??....

提前致谢!!

4

1 回答 1

0

为什么不首先检查您的碰撞矩形是否正确绘制在位置上。你可以通过调用来做到这一点

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

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

可能是您在错误的位置绘制碰撞矩形,因此永远不会发生碰撞。

于 2015-03-16T04:25:25.827 回答