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

2018/2/3

How can i use those classes (see in the topic) in my scenario

1
2
frequency frequency

2018/2/3

#
Hello, From this scenario i want to get http://www.greenfoot.org/scenarios/20784 the ScoreBoard and the Saver class. Scoreboard pops up an board which displays the score and the saver saves the bestscores in an CSV file. My scenario is this http://www.greenfoot.org/scenarios/20778 .Thanks!
danpost danpost

2018/2/3

#
I'll be checking out those classes.
danpost danpost

2018/2/4

#
I find that the Saver class is a bit obtuse. It is not written very well and it is also not really necessary. The method inside, if written properly, would be useful. A more proper way might be something like this:
public static void saveHighScore(int score)
{
    Player player = null;
    if ((UserInfo.isStorageAvailable())) player = UserInfo.getMyInfo();
    if (player == null) return;
    if (score > player.getScore() || (player.getInt(0) == 0))
    {
        player.setScore(score);
        player.setInt(0, 1);
        iplayer.store();
    }
}
Another method that would be useful is one to get the user's current high scores:
public static int[] getCurrentHighScores()
{
    Player player = null;
    if ((UserInfo.isStorageAvailable())) player = UserInfo.getMyInfo();
    if (player == null) return new int[] { 0, 0, 0 };
    return new int { player.getInt(0),  player.getInt(1),  player.getInt(2) };
}
You can put these in the MainMenu class to allow any class to set the value of or get the high scores for displaying as well as to get an individual high score in any class using, for example:
int highScore60 = MainMenu.getCurrentHighScores()[1];
frequency frequency

2018/2/4

#
Which one should i paste on where? Will i delete the saver class?
danpost danpost

2018/2/4

#
frequency wrote...
Which one should i paste on where?
danpost wrote...
You can put these in the MainMenu class
frequency wrote...
Will i delete the saver class?
Yes.
frequency frequency

2018/2/4

#
Here you say iPlayer and Player. http://prntscr.com/i9zci8 How can i modify it so that it uses my 3 classes? Thanks!
danpost danpost

2018/2/4

#
frequency wrote...
Here you say iPlayer and Player.
Should be 'player' (just missed the 'i' when doing a deletion of characters).
How can i modify it so that it uses my 3 classes? Thanks!
For getting a score, just change the '1' at the end of the 'int highScore60 = ' line I have shown above to a '0' (for 30) or a '2' (for 90). For saving -- oops -- change first line of 1st method to:
public static void saveHighScore(int which, int score)
Change line 6 to:
if (score > player.getInt(which))
Remove line 8 and change line 9 to:
player.setInt(0, which);
danpost danpost

2018/2/4

#
With the above changes, you can save a possible high score with (example for 60 game):
MainMenu.saveHighScore(1, score);
frequency frequency

2018/2/5

#
I got confused! This is the code on the MainMenu! Should i paste it into the 3 game mode score classes? Thanks!
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;
    MainMenu.saveHighScore(1, score);
    
    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);
            addObject(new Credits(),750,700);
            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, 1000, 50);
        text  = new GreenfootImage("60-second High Score: "+highScore60, 30, TEXT_COLOR, TRANSPARENT_COLOR);
        getBackground().drawImage(text, 1000, 504);
        text  = new GreenfootImage("90-second High Score: "+highScore90, 30, TEXT_COLOR, TRANSPARENT_COLOR);
        getBackground().drawImage(text, 1000, 570);
   }


   public static void saveHighScore(int which, int score)
{
    Player player = null;
    if ((UserInfo.isStorageAvailable())) player = UserInfo.getMyInfo();
    if (player == null) return;
    if (score > player.getInt(which))
    {
        
        player.setInt(0, which);
        iplayer.store();
    }
}






















}
What did i do wrong? I got confused!
danpost danpost

2018/2/5

#
In line 84, change 'Player' to 'UserInfo' (my bad). Leave 'player' alone. In line 91, remove the first letter -- the 'i'. Remove line 18 (that needs to be done elsewhere -- wherever game over is detected for game 60).
frequency frequency

2018/2/5

#
Done,compiled with no errors.Now
MainMenu.saveHighScore(1, score);
Where does this go?
danpost danpost

2018/2/5

#
frequency wrote...
Done,compiled with no errors.Now
MainMenu.saveHighScore(1, score);
Where does this go?
It goes in the classes with timers -- in the block where the timer decreases and its value is check against a zero value -- in those blocks.
frequency frequency

2018/2/5

#
On timer30 class CODE
import greenfoot.*;
import java.awt.Color;    
 
/**
 * an object that display the remaining time units before the scenario stops;
 * this class counts frames (or act cycles) as the regulating unit of time
 */
public class Timer30 extends Actor   
{      
    int time = 3; // seconds to run
    int fps = 55; // approximate number of frames per second (adjust as needed)
    int cycles = time * fps; // the countdown timer
    private static final Color TEXT_COLOR = new Color(255,255,255);
    private static final Color TRANSPARENT_COLOR = new Color(0,0,0,0);
    GreenfootSound sound = new GreenfootSound("DIE.wav");
    /*
     * sets the initial image of this Timer object
     */
    public Timer30()   
    {
        updateImage(); // shows remaining time
    }  
 
    /*
     * runs the timer and updates the image of this Timer object
     */
    public void act()   
    {    
        cycles--; // decrement countdown timer
        if (cycles % fps == 0)
        {
            updateImage();        // show the time remaining (only done when needed) 
           
        }
        if (cycles == 0)
        {
            Greenfoot.setWorld(new GameOverScreen());
            [b]MainMenu.saveHighScore(1,score);[/b]
                    
            sound.play();
        }
    }
      
    /*
     * sets the image to the number of approximate seconds remaining on the timer
     */
    private void updateImage()
    {
        setImage(new GreenfootImage("Seconds left " + (cycles+fps-1)/fps, 24,Color.RED, null));
        
    }
}
It will underline score. Also what does "1",mean?
danpost danpost

2018/2/5

#
'score' should probably be something like 'World30.score' or something like that. The '1' means game 60. In the class given, the 'Timer30' class, it should be a '0' (and in Timer90 -- a '2').
frequency frequency

2018/2/5

#
Is "World30.score" a variable that i should set somewhere? I got no idea what should i put :/ Scenario here http://www.greenfoot.org/scenarios/20781
There are more replies on the next page.
1
2