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

2018/2/1

Keep the best score on the main menu

1
2
frequency frequency

2018/2/1

#
Hello, What should i add to my code so as to keep the best score on the main menu. And if i beat it,to update? Thanks!
danpost danpost

2018/2/1

#
frequency wrote...
What should i add to my code so as to keep the best score on the main menu. And if i beat it,to update?
Add the following fields:
public static int highScore30, highScore60, highScore90;
and the following method:
public static void finalScore30(int score)
{
    if (score > highScore30) highScore30 = score;
}
with similar methods for 60 and 90.
frequency frequency

2018/2/1

#
Should i make new actors? Where do i paste it?
danpost danpost

2018/2/1

#
frequency wrote...
Should i make new actors? Where do i paste it?
No new actors. It goes in the MainMenu class. You can draw images of the text onto the world background of the menu, if you are to show them.
frequency frequency

2018/2/1

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

/**
 * Write a description of class MainMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainMenu extends World
{

    /**
     * Constructor for objects of class MainMenu.
     * 
     */
    public static int highScore30, highScore60, highScore90;
    public MainMenu()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1500,800,1); 
        Items();
        Intro();
        
    }
    void Items()
    {
        addObject(new thirty_seconds(),750,400);
        addObject(new sixty_seconds(),750,500);
        addObject(new ninety_seconds(),750,600);
        
    }
 
    void Intro()
    {
          GreenfootSound sound = new GreenfootSound("START.wav");
          Greenfoot.playSound("START.wav");
    }

    public static void finalScore30(int score)
    {
    if (score > highScore30) highScore30 = score;
    }
    
    
    public static void finalScore60(int score)
   {
    if (score > highScore60) highScore60 = score;
   }


    public static void finalScore90(int score)
    {
    if (score > highScore90) highScore90 = score;
    }



}
Cant see any change. Should i add a new object? I want those best scores to be on the main menu!
danpost danpost

2018/2/1

#
Either remove line 35 or change line 36 to:
sound.play();
Place the following at line 30:
HiScores();
and add the following method to the class (location of image drawing can be adjusted as needed):
private void HiScores()
{
    GreenfootImage text = new GreenfootImage("30-second High Score: "+highScore30, 30, TEXT_COLOR, TRANSPARENT_COLOR);
    getBackground().drawImage(text, 300, 50);
    text  = new GreenfootImage("60-second High Score: "+highScore60, 30, TEXT_COLOR, TRANSPARENT_COLOR);
    getBackground().drawImage(text, 400, 50);
    text  = new GreenfootImage("90-second High Score: "+highScore90, 30, TEXT_COLOR, TRANSPARENT_COLOR);
    getBackground().drawImage(text, 500, 50);
}
and copy paste the color constants into the class.
frequency frequency

2018/2/1

#
I get underlines on TEXT_COLOR AND TRANSPARENT_COLOR CODE
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class MainMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainMenu extends World
{

    /**
       * Constructor for objects of class MainMenu.
     * 
     */
    

    public static int highScore30, highScore60, highScore90;
    public MainMenu()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1500,800,1); 
        Items();
        Intro();
        HiScores();
    }
    
    void Items()
    {
        addObject(new thirty_seconds(),750,400);
        addObject(new sixty_seconds(),750,500);
        addObject(new ninety_seconds(),750,600);
        HiScores();
    }
    
    void Intro()
    {
          GreenfootSound sound = new GreenfootSound("START.wav");
          Greenfoot.playSound("START.wav");
    }

    public static void finalScore30(int score)
    {
    if (score > highScore30) 
    {
        highScore30 = score;
    }
    }
    
    
    public static void finalScore60(int score)
   {
    if (score > highScore60)
    {
        highScore60 = score;
    }
    }


    public static void finalScore90(int score)
    {
    if (score > highScore90)
    {
        highScore90 = score;
    }
    }
    
    private void HiScores()
    {
    GreenfootImage text = new GreenfootImage("30-second High Score: "+highScore30, 30, TEXT_COLOR, TRANSPARENT_COLOR);
    getBackground().drawImage(text, 300, 50);
    text  = new GreenfootImage("60-second High Score: "+highScore60, 30, TEXT_COLOR, TRANSPARENT_COLOR);
    getBackground().drawImage(text, 400, 50);
    text  = new GreenfootImage("90-second High Score: "+highScore90, 30, TEXT_COLOR, TRANSPARENT_COLOR);
    getBackground().drawImage(text, 500, 50);
}


}
danpost danpost

2018/2/2

#
frequency wrote...
I get underlines on TEXT_COLOR AND TRANSPARENT_COLOR
You did not do everything --
and copy paste the color constants into the class.
frequency frequency

2018/2/2

#
Hello! Can you please tell me what did i miss? Since i cant find it? Thanks!
danpost danpost

2018/2/2

#
frequency wrote...
Can you please tell me what did i miss? Since i cant find it? Thanks!
This:
private static final Color TEXT_COLOR = new Color(255,255,255);
private static final Color TRANSPARENT_COLOR = new Color(0,0,0,0);
frequency frequency

2018/2/2

#
New MAINMENU WORLD Code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class MainMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainMenu extends World
{

    /**
       * Constructor for objects of class MainMenu.
     * 
     */
    
    private static final Color TEXT_COLOR = new Color(255,255,255);
    private static final Color TRANSPARENT_COLOR = new Color(0,0,0,0);
    public static int highScore30, highScore60, highScore90;
    public MainMenu()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1500,800,1); 
        Items();
        Intro();
        
    }
    
    void Items()
    {
        addObject(new thirty_seconds(),750,400);
        addObject(new sixty_seconds(),750,500);
        addObject(new ninety_seconds(),750,600);
        
    }
    
    void Intro()
    {
          GreenfootSound sound = new GreenfootSound("START.wav");
          Greenfoot.playSound("START.wav");
    }

    public static void finalScore30(int score)
    {
    if (score > highScore30) 
    {
        highScore30 = score;
    }
    }
    
    
    public static void finalScore60(int score)
   {
    if (score > highScore60)
    {
        highScore60 = score;
    }
    }


    public static void finalScore90(int score)
    {
    if (score > highScore90)
    {
        highScore90 = score;
    }
    }
    
 


}
Still cant do it! Thank u!
danpost danpost

2018/2/2

#
frequency wrote...
Still cant do it!
What happened to the bulk of this?
frequency frequency

2018/2/2

#
Main menu code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class MainMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainMenu extends World
{

    /**
       * Constructor for objects of class MainMenu.
     * 
     */
    
    private static final Color TEXT_COLOR = new Color(255,255,255);
    private static final Color TRANSPARENT_COLOR = new Color(0,0,0,0);
    public static int highScore30, highScore60, highScore90;
    public MainMenu()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1500,800,1); 
        Items();
        Intro();
        
    }
    
    void Items()
    {
        addObject(new thirty_seconds(),750,400);
        addObject(new sixty_seconds(),750,500);
        addObject(new ninety_seconds(),750,600);
        
    }
    
    void Intro()
    {
          GreenfootSound sound = new GreenfootSound("START.wav");
          Greenfoot.playSound("START.wav");
    }

    public static void finalScore30(int score)
    {
    if (score > highScore30) 
    {
        highScore30 = score;
    }
    }
    
    
    public static void finalScore60(int score)
   {
    if (score > highScore60)
    {
        highScore60 = score;
    }
    }


    public static void finalScore90(int score)
    {
    if (score > highScore90)
    {
        highScore90 = score;
    }
    }
    
 


}
danpost danpost

2018/2/2

#
HiScores method is missing along with the call to it from act method.
frequency frequency

2018/2/2

#
Hello! It works but the high scores wont stay there. They go to 0. Also after we fix that i want to call it from a button. And the appear a new "screen" with a custom image saying the score!
There are more replies on the next page.
1
2