简体   繁体   中英

How to create multiple, identical objects that chase a single object [Java]

I have been having some trouble with attempting to create constantly "spawning" objects (called Rushers) that chase after a single object that is user-controlled (called Character). My main issue is the coordinates for the Rushers seem to be the exact same as that of the Character.

I have my code split into several classes. Here is the code for the Character first:

public class Gamepanel {

private boolean right = false, left = false, up = false, down = false;
public Character mainChar;
private static int xCoor = 230;
private static int yCoor = 210;

This is the main loop:

public void tick(){
    mainChar = new Character(xCoor, yCoor, 30, 50);
    mainChar.setxCoor(xCoor);
    mainChar.setyCoor(yCoor);

    if(right && mainChar.getxCoor() < 469) xCoor+=5;
    if(left && mainChar.getxCoor() > 0) xCoor -= 5; 
    if(up && mainChar.getyCoor() > 0) yCoor -= 5;
    if(down && mainChar.getyCoor() < 449) yCoor+=5;
}

The GFX:

mainChar.draw(g);

The controls are basic keyPressed and keyReleased methods. I will not post them for sake of brevity, as they seem to be working fine.

This is the code for the Character class:

public class Character {

int xCoor = 230;
int yCoor = 210;
int width = 30;
int height = 50;

public Character(int xCoor, int yCoor, int width, int height) {
    this.xCoor = xCoor;
    this.yCoor = yCoor;
}

public void draw(Graphics g) {
    g.setColor(Color.GREEN);
    g.fillRect(xCoor, yCoor, width, height);
}

public int getxCoor() {
    return xCoor;
}

public void setxCoor(int xCoor) {
    this.xCoor = xCoor;
}

public int getyCoor() {
    return yCoor;
}

public void setyCoor(int yCoor) {
    this.yCoor = yCoor;
}
}

These all appear to work as normal with no massive issues. The biggest problems I was having was with my other objects, the Rushers. Here is their code in the Gamepanel:

public class Gamepanel {

private Rusher rusher;
ArrayList<Rusher> rushers;
int spawnTimer = 0;

public Gamepanel() {
    rushers = new ArrayList <Rusher>();
}

public void tick(){
    spawnTimer++;
    if(spawnTimer > 75) {
        spawn();
        spawnTimer = 0;
    }

    if(rushers.size() > 0) {
        for(int i = 0; i < rushers.size(); i++) {
            rushers.get(i).tick();
        }
    }
}

The GFX:

if(rushers.size() > 0) {
    for(int i = 0 ; i < rushers.size(); i++) {
        rushers.get(i).draw(g);
    }
}

Spawn method (pretty sure the error is in here):

public void spawn() {
    int xSpawn[] = new int[4];
    int ySpawn[] = new int[4];
    ySpawn[0] = 250; ySpawn[1] = 499; ySpawn[2] = 250; ySpawn[3] = 0;
    xSpawn[0] = 0; xSpawn[1] = 250; xSpawn[2] = 499; xSpawn[3] = 250;
    int spawnCoor = randomRange(0, 3);
    rusher = new Rusher(xSpawn[spawnCoor], ySpawn[spawnCoor], 10, 10);
    rusher.setxCoor(xSpawn[spawnCoor]);
    rusher.setyCoor(ySpawn[spawnCoor]);
    rushers.add(rusher);
}

and finally, the Rusher class:

public class Rusher {

private static int xCoor;
private static int yCoor;
private int width = 20;
private int height = 20;

public Rusher(int xCoor, int yCoor, int width, int height) {
    super(xCoor, yCoor, width, height);
    Rusher.xCoor = xCoor;
    Rusher.yCoor = yCoor;
}

public void tick() {
    if(xCoor > Gamepanel.mainxCoor()) xCoor -= 2;
    if(yCoor > Gamepanel.mainyCoor()) yCoor -= 2;
    if(xCoor < Gamepanel.mainxCoor()) xCoor += 2;
    if(yCoor < Gamepanel.mainyCoor()) yCoor += 2;
}

public void draw(Graphics g) {
    g.setColor(Color.RED);
    g.fillRect(xCoor, yCoor, width, height);
}

public int getxCoor() {
    return xCoor;
}

public void setxCoor(int xCoor) {
    Rusher.xCoor = xCoor;
}

public int getyCoor() {
    return yCoor;
}

public void setyCoor(int yCoor) {
    Rusher.yCoor = yCoor;
}

}

Just to recap, the main issue is that the Rushers will spawn with the same coordinates as the Character, and will also move just as fast, despite me setting their motion to 3 slower than the Character. I tried to make this as brief as possible. If more code is needed, I do have some others that relate to these objects.

Thanks in advance.

I found it out. I was setting the coordinates for the Rushers as static, so they all shared the same ones. When I changed that, and adjusted some other, smaller things, it worked out much better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM