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

2014/9/20

Problem with resetting score to zero

1
2
3
Super_Hippo Super_Hippo

2014/9/23

#
danpost wrote...
Ok. Now remove lines 11 and 22 from the Start class and show your Counter class code.
DarkSoulDemon DarkSoulDemon

2014/9/23

#
This is the Counter code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
   private static int score;
   public static int level;
  
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
   {
      setImage(new GreenfootImage("Score: "+ score,24, Color.RED, Color.BLACK)); 
      // this sets the image to say Score but then add what ever the score is so it may look like this Score:10 and at the start it looks like Score:0
       
   }

   public void addScore()
    {
        score++;
        // this code adds a score of 1 each time an Rock is killed
        if (score == 3
        // need to make a level varible
        ){
          Greenfoot.setWorld(new L2());
          level=1;
          score= 4;
          
          // This code changes the back ground to level 2 when 3 points are scored
        } 
      
        if (score == 20){
          Greenfoot.setWorld(new L3());
          level =2;
          score=21;
          //This code changes the back ground to level 3 when 20 points are scored 
         }  
        
        if (score == 30){
          Greenfoot.setWorld(new Finished());
          score=0;
          //This code changes the back ground to the finished screen when 30 points are scored
         }
       
    }

     public void reset() 
   {
        score =0;
   }
   
}
danpost danpost

2014/9/24

#
Remove 'static' from line 13 (the Counter object created will be static in the World class and the 'score' field should belong to the Counter instance). Remove line 14 altogether (the level is stored as a static field in your Start class). Change line 20 from 'public void act()' to 'public void update()'. Then add a call to it at line 30, after 'score++;' in your 'addScore' method. Also, you can add the following so a greenfoot image does not appear for the actor before the game is started:
public Counter()
{
    update();
}
Lines 35 and 43 should be done in their respective level constructors, not here. Lines 36 and 44 are not needed as the score can only increment to those values but one time. Line 50 and the 'reset' method, lines 56 through 59 are not needed because setting a new Level1 world should reset the value from its constructor.
DarkSoulDemon DarkSoulDemon

2014/9/24

#
Thank you for being so patient with me thank you very much it is really appreciated thank you very much.
You need to login to post a reply.
1
2
3