如何在libgdx

时间:2016-05-23 18:06:03

标签: java libgdx

在我使用libgdx开发的小游戏中,我将矛作为游戏对象。 我使用Scene2D实现它们,作为Actor的子类。长矛可以旋转90度。我想把我的形象中的“speartip”部分作为“伤害”玩家的一部分,轴应该是无害的。因此,当我构造我的Spear物体时,我还构造了2个矩形,覆盖了矛的尖端和轴。但是当我的演员使用setRotation()进行旋转时,矩形显然不会,因为它们没有“附着”到矛物体上。

你对如何处理这类东西有什么建议吗?代码如下。

    public TrapEntity(Texture texture, float pos_x, float pos_y, float width, float height, Vector2 center,
                  float rotation, Rectangle hurtZone, Rectangle specialZone) {
    super(texture, pos_x, pos_y, width, height, center, rotation);

    this.hurtZone = new Rectangle(hurtZone.getX(), hurtZone.getY(), hurtZone.getWidth(), hurtZone.getHeight());
    this.specialZone = new Rectangle(specialZone.getX(), specialZone.getY(), specialZone.getWidth(), specialZone.getHeight());

}

同一类中的render方法。我用它来渲染“hurtZone”矩形的边界:

    @Override
public void draw(Batch batch, float alpha){
    super.draw(batch, alpha);
    batch.end();
    sr.setProjectionMatrix(batch.getProjectionMatrix());
    sr.setTransformMatrix(batch.getTransformMatrix());
    sr.begin(ShapeRenderer.ShapeType.Line);
    sr.setColor(Color.RED);
    sr.rect(hurtZone.getX(), hurtZone.getY(), hurtZone.getWidth(), hurtZone.getHeight());
    sr.end();
    batch.begin();

}

1 个答案:

答案 0 :(得分:1)

矩形不能旋转,我不得不做类似的事情并努力寻找解决方案。我发现的最佳解决方案是使用Polygon。你会做这样的事情;

    //Polygon for a rect is set by vertices in this order
    Polygon polygon = new Polygon(new float[]{0, 0, width, 0, width, height, 0, height});

    //properties to update each frame
    polygon.setPosition(item.getX(), item.getY());
    polygon.setOrigin(item.getOriginX(), item.getOriginY());
    polygon.setRotation(item.getRotation());

然后,检查点是否在旋转的Polygon使用范围内;

    polygon.contains(x, y);
相关问题