This site requires JavaScript, please enable it in your browser!
Greenfoot back
karolis
karolis wrote ...

2014/3/27

Need help decrementing Lives

karolis karolis

2014/3/27

#
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--;
    }
}
danpost danpost

2014/3/28

#
Please show the code in the other Actor subclass where you are trying to decrement the number of lives.
danpost danpost

2014/3/28

#
Please show the code in the other Actor subclass where you are trying to decrement the number of lives. Actually, there may be an easier way to do what you are trying to do in this class. It seems a bit awkward to have more than one Lives object have the same value for 'currentLives'. It would seem more useful for each to hold a life number instead.
import greenfoot.*;

public class Lives extends Actor
{
    public static int CurrentLives;
    public int LifeNumber;
     
    public Lives()
    {
        this(1);
        CurrentLives = 3;
    }

    public Lives(int LifeNum)
    {
        LifeNumber = LifeNum;
        setRotation(270);
    }

    public void act() 
    {
        int LivesCount = getWorld().getObjects(Lives.class).size();
        while (LivesCount < CurrentLives) getWorld().addObject(new Lives(++LivesCount),LivesCount*50+50, 50);
        if (CurrentLives < LifeNumber)
        {
            if (LifeNumber == 1) main.GameOver = true;
            getWorld().removeObject(this);
        }
    }
}
karolis karolis

2014/3/28

#
    public void Collision()
    {
        Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class);
        if (asteroid != null)
        {
            Lives.CurrentLives--;
        }
    }
but your code works perfectly, thank you! i am still unaware of some of the coding techniques.. what exactly does the '.size()' do? i am unsure about is how to write text on the screen, may i ask for some hints? Another problem that im having is with statics again, I cannot execute
public static void Appear(int AsteroidX, int AsteroidY)
    {
        getWorld().addObject(new PowerUp(), AsteroidX, AsteroidY);
    }
because "non-static method getWorld() cannot be referenced from a static context" but i need this method to be static, otherwise this line cannot execute it:
if (Greenfoot.getRandomNumber(PowerUpDropChance) == PowerUpDropChance) PowerUp.Appear( getX(), getY() );
from my Asteroid class
danpost danpost

2014/3/28

#
getWorld: method of the Actor class that returns the World object that the actor is in (null is returned if the actor is not in a world) getObjects: method of the World class that returns a List object containing all objects created from the given class that are in a world (or all objects if the argument given is null) size: method of the List class that returns the number of elements contained within a list By checking the API documentations on these classes, you can trace any dot notational expression from the first part to the end part and determine what it returns. Actor>World>List>int. Each part is processed by a method of its class.
You need to login to post a reply.