Ive tried looking at many different classes, but I just cant get this to work
What I want to do how older games represented the number of lives left, you pick up a life, and an icon is added at the top of the screen, each icon representing 1 life. You lose 1 life and it is decremented, like a stack.
I call this code at the beginning from my 'Main' World, which adds 1 life, then this code adds the other 2 once the play button is pressed.
However i am not able to decrement the lives. The 'CurrentLives' is decremented by another actor, as i wasnt able to call the method 'RemoveLife' because 'RemoveLife' is not a static method and cannot be referenced from a static context, which i dont really understand.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Lives extends Actor
{
public int StartLives = 3;
public static int CurrentLives;
public static int LivesDisplayed;
public Lives()
{
setRotation(270);
LivesDisplayed = 1
CurrentLives = StartLives;
}
public Lives(int CurrentLives)
{
setRotation(270);
}
/**
* Act - do whatever the Lives wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (CurrentLives <= 0)
{
main.GameOver = true;
}
if (LivesDisplayed < CurrentLives)
{
AddLife();
}
else if (CurrentLives != LivesDisplayed)
{
RemoveLife();
}
}
public void AddLife()
{
while (LivesDisplayed < CurrentLives)
{
Lives lives = new Lives(CurrentLives);
getWorld().addObject(lives, (LivesDisplayed * 50) + 50, 50);
LivesDisplayed++;
}
}
public void RemoveLife()
{
Actor Lives = getOneObjectAtOffset( (LivesDisplayed * 50) + 50 , 50, Lives.class);
if (Lives != null) getWorld().removeObject(this);
LivesDisplayed--;
}
}
