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

share|improve this question

closed as off-topic by Anko, bummzack, Trevor Powell, Byte56 May 29 at 16:46

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Trevor Powell, Byte56
  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Anko, bummzack
If this question can be reworded to fit the rules in the help center, please edit the question.

3  
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

3 Answers 3

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:

//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.

share|improve this answer

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

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

share|improve this answer
2  
Information about why this is a problem and how this solves it makes for a better answer. –  Byte56 May 24 at 14:48

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