java为什么当所有其他精灵移动时,Carpaint不移动?
我有一个在动画中移动精灵的代码——除了我的一个精灵外,其他所有精灵都会移动,我不知道为什么。有人能解释一下吗
我的精灵课:
abstract class SimpleSprite {
// basic x,y movement,keeps a master list of Sprites
public static final ArrayList<SimpleSprite> sprites = new ArrayList<SimpleSprite>();
float x, y, dx, dy; // position and velocity (pixels/TIMER_MSEC)
public SimpleSprite(float x, float y, float dx, float dy) {
// initial position and velocity
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
sprites.add(this);
}
public void update() { // update position and velocity every n milliSec
// default - just move at constant velocity
x += dx; // velocity in x direction
y += dy; // velocity in y direction
}
abstract public void draw(Graphics2D g2d);
// just draw at current position, no updating.
}
我的雪碧坏了:
class Carpaint extends SimpleSprite {
public Carpaint(float x, float y, float dx, float dy) {
super(x, y, dx, dy);
}
@Override
public void draw(Graphics2D g2d){
g2d.setColor(Color.pink);
g2d.fillRect(50, 50, 40, 60);
g2d.setColor(Color.black);
g2d.drawRect(50, 50, 40, 60);
g2d.drawRect(60, 60, 20, 40);
g2d.fillRect(45, 50, 5, 15);
g2d.fillRect(90, 50, 5 , 15);
g2d.fillRect(45, 95, 5, 16);
g2d.fillRect(90, 95, 5, 16);
}
}
我的主要意见是:
public static void main(String[] args) {
// create and display the animation in a JFrame
final JFrame frame = new JFrame("Animation 2 (close window to exit)");
Animation2 animationPanel = new Animation2(600, 500);
frame.add(animationPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// add some sprites...
new Square(0, 0, 3, 2, 40);
new Ball(500, 0, -3, 3, 20);
new Ball(0, 500, 2, -5, 30);
new Carpaint(100, 100, 20, -6);
}
我希望所有的精灵都能移动,但汽车却不能(参见此处:https://i.gyazo.com/0219127277d2543735b3a4727e7c7e72.mp4)
# 1 楼答案
答案来自@realpoinsist——我并不是在用
x
和y
来粉刷汽车。 新代码: