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

2013/3/13

need help with updating a score board

sophia8123 sophia8123

2013/3/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class level1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class level1 extends World


{
private int score = 0;
private int lives = 5; 
    /**
     * Constructor for objects of class level1.
     * 
     */
    public level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
            addObject(new Shark(),115,314);
            addObject(new Fish3(),494,237);
            addObject(new Fish3(),480,81);
            addObject(new Fish3(),119,93);
            addObject(new Fish3(),313,181);
            addObject(new Fish3(),395,324);
            addObject(new Fish3(),323,71);
            addObject(new StatusBoard(),40,385);
            StatusBoard.updateStatus(score,lives);
            
        
    }
    
    public void increaseScore (int points)
    {
        score += points;
        StatusBoard.updateStatus(score,lives);
        
       
    }
    //public void increaseScore(int points){ 
        
      
   }
        
    public int getScore ()
    {
        return score;
    }
        
    public void loseLife ()
    {
        lives = lives - 1;
             
    }
        
        public int getLives()
        {
            return lives;
        }}
Gevater_Tod4711 Gevater_Tod4711

2013/3/13

#
It would be helpfull to know what is the problem with this code.
danpost danpost

2013/3/13

#
Line 58 should probably be:
StatusBoard.updateStatus(score, lives);
And I would have to assume that the 'updteStatus' method is declared with
public static void updateStatus(int score, int lives)
and that both 'score' and 'lives' are declared 'static' as well. If you are not using static methods and fields, then you need to get a reference to the statusboard object that you had created. Use
StatusBoard statusBoard = (StatusBoard)getObjects(StatusBoard.class).get(0);
statusBoard.updateStatus(score, lives);
You need to login to post a reply.