Hi,
I'm trying to make a Nazi Zombie shooter in 2D and I'm having a bit of trouble making the bullets (which are in an arrayList) shoot in the direction that the player is facing/last moved in.
I was told to make a variable in the move() function in the player class to work out which button was pressed (out of UP,DOWN,LEFT,RIGHT) and to then call it in the main program, like this:
void move(int direction) {
if(direction == UP) {
y = y - 8;
} else if(direction == DOWN) {
y = y + 8;
} else if(direction == RIGHT) {
x = x + 8;
} else if(direction == LEFT) {
x = x - 8;
}
}
And call it in the main program:
void keyPressed() {
if(key == CODED) {
if(keyCode == UP) {
player.move(UP);
player.y = constrain(player.y,15,height);
} else if (keyCode == DOWN) {
player.move(DOWN);
player.y = constrain(player.y,0,height-17);
} else if (keyCode == LEFT) {
player.move(LEFT);
player.x = constrain(player.x,3,width);
} else if (keyCode == RIGHT) {
player.move(RIGHT);
player.x = constrain(player.x,0,width-17);
}
}
}
But then I have my b.move(); in the bullet arrayList in the main program:
void updateBullets() {
ArrayList<Bullet> bulletsToDelete = new ArrayList<Bullet>();
for (Bullet b: bullets) {
b.move();
if (b.x - 1 > 500 || b.x < 0 || b.y > 500 || b.y < 0) {
bulletsToDelete.add(b);
}
b.display();
}
for (Bullet b: bulletsToDelete) {
bullets.remove(b);
}
}
However the b.move(); calls this function in the bullet class:
void move() {
}
I don't know what to put in here in order for the bullets to be shot in the direction the player last moved in! :(
I will be really pleased with any feedback! Thanks! :D
1