0

好吧,我用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++;

为什么它也能动枪?

4

1 回答 1

2

在调用子弹上的 setPosition 方法之前,您需要克隆枪的位置对象。

Point2D currentGunPos = (Point2D)sGun.position().clone();
sBullet[bulletNum].setPosition(currentGunPos);
于 2011-02-26T21:51:30.333 回答