有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

graphics2d我用Java制作的游戏出于某种原因连接了两个精灵?

我用爪哇制作了这个游戏,当你射出一颗子弹时,它会移动枪和子弹。下面是我认为问题所在的代码块:

case KeyEvent.VK_SPACE:
        Point2D currentGunPos = sGun.position();
        sBullet[bulletNum].setAlive(true);
        sBullet[bulletNum].setPosition(sGun.position());
        sBullet[bulletNum].setVelocity(new Point2D(-5,0));
        bulletNum++;

为什么它也会移动枪


共 (1) 个答案

  1. # 1 楼答案

    在对子弹调用setPosition方法之前,需要克隆枪的position对象

    Point2D currentGunPos = (Point2D)sGun.position().clone();
    sBullet[bulletNum].setPosition(currentGunPos);