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

2013/4/26

Need little help with live counter

103163 103163

2013/4/26

#
Hi I am working with Frogger for school, and I need a live counter. I came far, but I don't know how to make the lives go down or up.
import greenfoot.*;  
import java.awt.Color;  
  
public class Lifeboard extends Actor  
{  
    int lives = 3;  
      
    public Lifeboard()  
    {  
        updateBoard();  
    }  
      
    private void updateBoard()  
    {  
        setImage(new GreenfootImage("Lives remaining: " + lives, 20, Color.black, new Color(0, 0, 0, 0)));  
    }  
      
    public void add(int addVal)  
    {  
        if (lives == 0 && addVal == -1)  
        { // ^ (lost a life and no lives remaining)  
            // play sound, showGameOver/Final score  
            // whatever you want to do for ending game  
            Greenfoot.stop();  
            return;  
        }  
        lives += addVal; updateBoard();  
    }  
      
    public int getLivesLeft()  
    {  
        return lives;  
    }  
    
    public void livesDown(){

        lives = lives -1;
        if( lives == 0){
            
            Greenfoot.stop();  
            return;
        }          
    }
}  
Please help me.
Gevater_Tod4711 Gevater_Tod4711

2013/4/26

#
In your world you need a Lifeboard object. That will count your lives. You just need to call the methods add or livesDown to increment or decrement the live value. To execute the methods you need to get the reference to the lifeboard in every class you want to execute this methods. To see how you can get these references you should do the Greenfoot Tutorials. Especially this one.
103163 103163

2013/4/27

#
I got the live counter in the world, but it isn't working. When the Car hits the Frog, the code is:
Greenfoot.Stop();
How do I get that if the Car hits the Frog, the live counter counts down?
Gevater_Tod4711 Gevater_Tod4711

2013/4/27

#
Like I said you need the reference to the live counter object. Try to do something like this:
LiveCounter counter = (LiveCounter) getWorld().getObjects(LiveCounter.class).get(0);
counter.livesDown();
If you just want it to work this code probably is enough. But if you want to understand what the code doe's you should do this greenfoot tutorial.
103163 103163

2013/4/27

#
So this piece of code goes in the World or in the Lifeboard I gave above?
Gevater_Tod4711 Gevater_Tod4711

2013/4/27

#
This code is executed when you want your character to loose one life.
103163 wrote...
I got the live counter in the world, but it isn't working. When the Car hits the Frog, the code is:
Greenfoot.Stop();
I think that would be the correct place for the code.
103163 103163

2013/4/27

#
It says: cannot find symbol - Class LifeCounter How do i help this?
danpost danpost

2013/4/27

#
The code should go where you have detected the Car hitting the Frog. The name of the class/object is 'Lifeboard', not 'LifeCounter, so you need to adjust the code for that.
103163 103163

2013/4/27

#
It isn't working, this is the piece with the Car,
public void lookForLeerling()
    {
        if ( canSee(Leerling.class) ) 
        {
            eat(Leerling.class);
            
            Lifeboard board = (Lifeboard) getWorld().getObjects(Lifeboard.class).get(0);  
            board.livesDown();            
        }
    }
This is the piece in the Lifeboard:
import greenfoot.*;  
import java.awt.Color;  
  
public class Lifeboard extends Actor  
{  
    int lives = 3;  
          
    public Lifeboard()  
    {  
        updateBoard();  
    }  
      
    private void updateBoard()  
    {  
        setImage(new GreenfootImage("Lives remaining: " + lives, 20, Color.black, new Color(0, 0, 0, 0)));  
    }  
      
    public void add(int addVal)  
    {  
        if (lives == 0 && addVal == -1)  
        { // ^ (lost a life and no lives remaining)  
            // play sound, showGameOver/Final score  
            // whatever you want to do for ending game  
            Greenfoot.stop();  
            return;  
        }  
        lives += addVal; updateBoard();  
    }  
      
    public int getLivesLeft()  
    {  
        return lives;  
    }  
    
    public void livesDown(){

        lives = lives -1;
        if( lives == 0){
            
            Greenfoot.stop();  
            return;  
        }          
    }    
}  
And this the one for the World:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Dit is de wereld waarin jullie gaan werken,
 * deze heeft als achtergrond al het uiterlijk 
 * van het spelletje meegekregen, dus deze hoeven 
 * jullie niet zelf meer te maken.
 * 
 * @author Justin Post
 * @version 1.0
 */
public class Wereld extends World
{
   
    
    public Wereld()
    {    
        // Maak een nieuwe wereld ter grootte van 800x600 blokken met elk blok 1 pixel
        super(800, 600, 1); 
        populateWorld();
    }
    /** code voor wereld en voor het autmatisch laten zien van het mannetje en auto's*/
    
    public void populateWorld()
    {
        addObject(new Leerling(), 400, 570);
        
        addObject(new Car1(), 50, 120);
        addObject(new Car1(), 250, 120);
        addObject(new Car1(), 450, 120);        
        addObject(new Car1(), 650, 120);
        
        
        addObject(new Car2(), 150, 190);
        addObject(new Car2(), 350, 190);
        addObject(new Car2(), 550, 190);
        addObject(new Car2(), 750, 190);
       
        addObject(new Car1(), 50, 450);
        addObject(new Car1(), 250, 450);
        addObject(new Car1(), 450, 450);
        addObject(new Car1(), 650, 450);
        
        addObject(new Car2(), 150, 510);
        addObject(new Car2(), 350, 510);
        addObject(new Car2(), 550, 510);
        addObject(new Car2(), 750, 510);        
     
        addObject(lifeboard, 500, 570); 
        
    }
    /** laten zien voor lifeboard*/
    
       public Lifeboard lifeboard = new Lifeboard();
}
Why isn't it working? Can someone see the problem?
Gevater_Tod4711 Gevater_Tod4711

2013/4/27

#
Do you get any compiler errors or exceptions?
danpost danpost

2013/4/27

#
You need to 'updateBoard' at any place the value of 'lives' changes.
derp2000 derp2000

2013/6/16

#
Do you need to put any code in the Leerling Class??
derp2000 derp2000

2013/6/16

#
danpost wrote...
You need to 'updateBoard' at any place the value of 'lives' changes.
And what do you mean whit this??
Gevater_Tod4711 Gevater_Tod4711

2013/6/16

#
If you update the value but don't update the board the value will be changed but you will not be able to see that the value changed because the image of the LiveCounter will not be updated. I think you should call the method updateBoard at the end of your livesDown() method.
You need to login to post a reply.