【问题标题】:Arrayed Rectangle Rotation阵列矩形旋转
【发布时间】:2014-01-03 17:31:56
【问题描述】:

我正在尝试制作 2D 游戏。我有一组矩形来表示攻击。我试图让它们旋转,所以它们都旋转了 45 度。当我尝试在第一个之后渲染更多法术时,它们会在屏幕周围的随机位置出现故障。这是我的代码:

Rectangle[] waterBolt = new Rectangle[10];
float[] wba = new float[10];
int wbc = 0;
Graphics2D[] gwb = new Graphics2D[10];

public void renderSpell(Graphics2D g) {
    for(int i=0; i<10; i++) {
        if (waterBolt[i] != null) {
            gwb[i] = (Graphics2D) g;
            gwb[i].rotate(Math.toRadian(45), waterBolt[i].x, waterBolt[i].y);
            gwb[i].fill(waterBolt[i]);
        }
    }
}

public void castSpell(int spellID) {
    waterBolt[wbc] = new Rectangle(playerX, playerY, 16, 16);
    wba[wbc] = (float) Math.toRadians(Math.atan2(mouseX - playerX, mouseY - playerY));
    wbc++;
    if (wbc >= 10) {
        wbc = 0;
    }
}

当我将它们全部静止投射时,我的屏幕上会发生以下情况:

每次我点击投射时,它们都会多旋转 45 度,但我不知道如何解决。

【问题讨论】:

    标签: java arrays rotation game-engine rectangles


    【解决方案1】:

    请记住,当您致电 gwb[i] = (Graphics2D) g; 时,您不会复制 g。相反,您的每个矩形都被您之前所有旋转的累积和旋转。试试这样的:

    public void renderSpell(Graphics2D g) {
        AffineTransform transform = g.getTransform();
        for(int i=0; i<waterBolt.length; i++) {
            if (waterBolt[i] != null) {
                g.rotate(Math.toRadian(45), waterBolt[i].x, waterBolt[i].y);
                g.fill(waterBolt[i]);
                g.setTransform(transform); // back to original orientation
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      相关资源
      最近更新 更多