我有一个由下一个代码定义的实体Actor:
this.world = world;
this.texture = texture;
// Create the body.
BodyDef def = new BodyDef(); // (1) Give it some definition.
def.position.set(x, y + 0.5f); // (2) Position the body on the world
def.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(def); // (3) Create the body.
// Now give it a shape.
PolygonShape box = new PolygonShape(); // (1) We will make a polygon.
Vector2[] vertices = new Vector2[6];
vertices[0] = new Vector2(0.04f , 0.24f );
vertices[1] = new Vector2(0.64f , 1.18f );
vertices[2] = new Vector2(1.66f , 1.8f);
vertices[3] = new Vector2(1.92f , 1.52f);
vertices[4] = new Vector2(1.18f , 0.66f);
vertices[5] = new Vector2(0.26f , 0.03f);
box.set(vertices); // (4) And put them in the shape.
fixture = body.createFixture(box, 3); // (5) Create the fixture.
fixture.setUserData("actor"); // (6) And set the user data to enemy.
box.dispose(); // (7) Destroy the shape when you don't need it.
// Position the actor in the screen by converting the meters to pixels.
setPosition((x - 0.5f) * PIXELS_IN_METER, y * PIXELS_IN_METER);
setSize(PIXELS_IN_METER, PIXELS_IN_METER);
this.setDebug(true);
当我添加最后一行setDebug时,我的Actor被一个正方形包围,我的命中率影响到这个正方形,而不是我的多边形定义的真实形状。此命中检测被触摸屏(InputProcessor)事件捕获:
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector2 coord = stage.screenToStageCoordinates(new Vector2((float)screenX,(float)screenY));
Actor hitActor = stage.hit(coord.x,coord.y,false);
if(hitActor != null) {
Gdx.app.log("myapp", "hit!!!!!");
}
return true;
}
我做错了什么?我只想检测我的游戏演员项目的命中率。提前感谢!!
Scene2D actor和Box2D主体不相同-如果您想看到主体正在调试,则必须使用Box2D的DebugRenderer
//creating debug renderer in show method or as class'es field
Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
//render() method
debugRenderer.render(world, camera.combined);
然后你会发现你的身体形状和演员的形状不一样。更重要的是,它们可能不会有相同的位置,因为由于身体的原因,您没有更新演员的位置(您应该在演员的actoverriden方法中使用类似setPosition(body.getPosition().x, body.getPosition().y);
-的东西来执行此操作,请记住,当身体的原点在中心时,演员的原点在左下角!)。
不幸的是,演员只能是矩形的,所以不可能创造出和身体相同的形状。但是,您可以将演员用作边界框,并将其大小设置为覆盖整个身体(如果身体旋转,它将发生变化!)。
另一个解决方案不是在主体上捕获侦听器,而是实现一些机制,比如在点击并检查冲突后创建主体