I'm making a space invaders game and I want to put in a lives counter. I did this with a lives_left class which is a child of the player class. the player class has a method that returns the number of lives left. the problem is when I call the method from the lives_left class it always returns the same value, no matter how many times the player dies.
this is the code for the lives_left class
this is the code for the player class
private int count1;
private int life1;
public lives_left(int count) {
player.scale(player.getWidth()*2,player.getHeight()*2);
setImage(player);
count1 = count;
}
public void act()
{
life1 = life();
if (life1 < count1){
getWorld().removeObject(this);
}
} GreenfootImage player = new GreenfootImage("Player.png");
GreenfootImage destroyed = new GreenfootImage("Player_destroyed_1.png");
private boolean spaceDown;
private int delay = 0 ;
public int lives;
public Player(){
player.scale(player.getWidth()*2,player.getHeight()*2);
destroyed.scale(destroyed.getWidth()*2,destroyed.getHeight()*2);
setImage(player);
lives = 3;
}
public void act()
{
collisionDetection();
//moovement controls
if(Greenfoot.isKeyDown("right")){
move(3);
}
if(Greenfoot.isKeyDown("left")){
move(-3);
}
//checks if there are less than two bullets if there are it lets you shoot bullets
if(getWorld().getObjects(playerBullet.class).size()<2){
if (!spaceDown && Greenfoot.isKeyDown("space")){
spaceDown = true;
getWorld().addObject(new playerBullet(), getX(), getY()-20);
}
if (spaceDown && !Greenfoot.isKeyDown("space")){
spaceDown = false;
}
}
}
private void collisionDetection(){
Actor a = getOneIntersectingObject(alienBullet.class);//sets any intersecting object to the variable a
if(a != null){//if there is an object in a then kill the player
getWorld().removeObject(a);
death();
}
}
private void death(){
setImage(destroyed);
getWorld().removeObjects(getWorld().getObjects(alienBullet.class));//removes all alien bullets
if(lives!=0){//if you have lives left it delays things a bit
Greenfoot.delay(100);
lives = lives - 1;
setImage(player);
}else{//if you have no lives left then you lose
setImage(destroyed);
Greenfoot.delay(100);
Greenfoot.setWorld(new Game_Over());
}
}
public int life(){
return lives;
}
