我有一个要随机移动的实体数组列表。不管我做什么,他们都不会动。这是我的女班:
public class Female extends Entity {
static int femaleX = 0;
static int femaleY = 0;
double walkSpeed = .1f;
Random rand = new Random();
int random;
int dir;
Player player;
public Female(int posX, int posY) {
super(posX, posY);
}
public void update() {
posX += femaleX;
posY += femaleY;
moveFemale();
}
public void draw(Graphics2D g2d) {
g2d.drawImage(getFemaleImg(), posX, posY, null);
if (Player.showBounds == true) {
g2d.draw(getBounds());
}
}
public Image getFemaleImg() {
ImageIcon ic = new ImageIcon("res/female.png");
return ic.getImage();
}
public Rectangle getBounds() {
return new Rectangle(posX, posY, getFemaleImg().getHeight(null),
getFemaleImg().getWidth(null));
}
public void moveFemale() {
random = rand.nextInt(3);
System.out.println(random);
if (random == 0) {
dir = 0;
posX -= (int) walkSpeed;
}
}
}
以下是我在主班中更新女班的方法:
public void actionPerformed(ActionEvent ae) {
player.update();
for(int i = 0; i < females.size(); i++){
Female tempFemale = females.get(i);
tempFemale.update();
}
repaint();
}
如果我做这样的事情(在女性更新方法中):
public void update() {
posX += femaleX;
posY += femaleY;
posX -= walkSpeed;
}
角色移动没有问题。为什么是这样?