Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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) {

public Image getFemaleImg() {
    ImageIcon ic = new ImageIcon("res/female.png");
    return ic.getImage();

public Rectangle getBounds() {
    return new Rectangle(posX, posY, getFemaleImg().getHeight(null),

public void moveFemale() {
    random = rand.nextInt(3);

    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) {

    for(int i = 0; i < females.size(); i++){
        Female tempFemale = females.get(i);

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?

share|improve this question
Dude, what's with the females? Are you objectifying them? That's a danger zone! –  Nick Wiggill Oct 28 '12 at 18:01
-__- thanks for your help.. they were just part of a game! –  opiop65 Oct 28 '12 at 18:10
add comment

2 Answers

up vote 2 down vote accepted

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:

float ax = 0;
float ay = 0;
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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.