# ArrayList of Entites Random Movement [closed]

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?

• Dude, what's with the females? Are you objectifying them? That's a danger zone! – Engineer Oct 28 '12 at 18:01
• -__- thanks for your help.. they were just part of a game! – opiop65 Oct 28 '12 at 18:10

Because (prior to your fix) that's exactly what you've asked it to do. Nowhere do I see femaleX or femaleY being updated; thus, they remain zero, and thus you add zero to the current position, so nothing moves.

I think you are not clear on what your code is supposed to be doing, here is how this is always meant to be approached:

//acceleration
float ax = 0;
float ay = 0;
//speed
float sx = 0;
float sy = 0;
//displacment AKA position
float x = 0;
float y = 0;

...

void update(...)
{
//after all input and AI, etc. etc., finalise your physics
//using the basic Equations of Motion a la Isaac Newton:
//new velocity is old velocity plus current acceleration (usually derived from inputs)
sx += ax;
sy += ay;
//new position is old position plus current speed
x += sx;
y += sy;
}


Whereas in your code, it almost seems as if you are saying position = position + someOtherPosition, which cannot really work. Speed (or as a vector, velocity) is the diff you apply to displacement, just as acceleration is the diff you apply to speed.

PS. Using acceleration is completely up to you -- you can just set velocities in your input handler code if you like. Many games do not need acceleration.

You cast walkSpeed to an int before adding it to the position in moveFemale(), its value will be 0 so nothing changes.

You have to call moveFemale(); in the update-method.

• Information about why this is a problem and how this solves it makes for a better answer. – MichaelHouse May 24 '14 at 14:48