ArrayList of Entites Random Movement
Posted
by
opiop65
on Game Development
See other posts from Game Development
or by opiop65
Published on 2012-10-28T17:47:35Z
Indexed on
2012/10/28
23:23 UTC
Read the original article
Hit count: 205
java
I have an arraylist of entites that I want to move randomly. No matter what I do, they won't move. Here is my female class:
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.Random;
import javax.swing.ImageIcon;
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;
}
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;
}
}
}
And here is how I update the female class in the main class:
public void actionPerformed(ActionEvent ae) {
player.update();
for(int i = 0; i < females.size(); i++){
Female tempFemale = females.get(i);
tempFemale.update();
}
repaint();
}
If I do something like this(in the female update method):
public void update() {
posX += femaleX;
posY += femaleY;
posX -= walkSpeed;
}
The characters move with no problem. Why is this?
© Game Development or respective owner