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

2014/5/24

Leaderboard

1
2
adi110 adi110

2014/5/24

#
I have a button in my menu (leaderboard ) in which i want to save my best 5 scores. How i can do that??? thanks
adi110 adi110

2014/5/24

#
i meant when people play the game, they can enter their name, and when they finish playing, their score will be saved in leaderbourd (which is on menu screen) so that when they click on it, they can see other peoples' scores who played the game
danpost danpost

2014/5/25

#
If this is for on the site, then select 'Edit>Import class...' from the menu bar and select the 'ScoreBoard' class and click on the 'Import' button. Then for the action when the leaderboard button on your menu is pressed, create a new ScoreBoard object; but, only if storage is available (refer to the UserInfo class API documentation on this).
adi110 adi110

2014/5/25

#
danpost can you write the code?? i don't understant what do you want to mean by "Then for the action when the leaderboard button on your menu is pressed, create a new ScoreBoard object; but, only if storage is available (refer to the UserInfo class API documentation on this)."
adi110 adi110

2014/5/25

#
I have done the first thing (If this is for on the site, then select 'Edit>Import class...' from the menu bar and select the 'ScoreBoard' class and click on the 'Import' button.) but i don't understand what to do next This is my function for increasing the score and i already have a the counter for keeping the score when my snake eat something public void Eat() { Actor fln; Actor hambur; Actor chip; Actor pzz; Actor apl; fln = getOneIntersectingObject(flan.class); hambur = getOneIntersectingObject(hamburger.class); chip = getOneIntersectingObject(chips.class); pzz = getOneIntersectingObject(pizza.class); apl = getOneIntersectingObject(apple.class); if(fln != null) { World world = getWorld(); Greenfoot.playSound("slurp.wav"); world.removeObject(fln); SpawnNewFlan(); Eaten(1); Score++; if(Score % 3 == 0) {SpawnNewHamburger();} if(Score % 10 == 0) {SpawnNewPizza();} if(Score % 6 == 0) {SpawnNewChips();} } else { if(hambur != null) { World world = getWorld(); Greenfoot.playSound("slurp.wav"); world.removeObject(hambur); Eaten(3); Score += 3; Countt++; if(Countt % 4 == 0) {SpawnNewApple();} if(Score % 10 == 0) {SpawnNewPizza();} if(Score % 6 == 0) {SpawnNewChips();} } else { if(chip != null) { World world = getWorld(); Greenfoot.playSound("slurp.wav"); world.removeObject(chip); timerStarted = true; moveAmount += 2; } else { if(pzz != null) { World world = getWorld(); Greenfoot.playSound("slurp.wav"); world.removeObject(pzz); Eaten(2); Score += 2; timerrStarted = true; moveAmount = 0; if(Score % 10 == 0) {SpawnNewPizza();} if(Score % 6 == 0) {SpawnNewChips();} } else { if(apl != null) { World world = getWorld(); Greenfoot.playSound("slurp.wav"); world.removeObject(apl); Eaten(5); Score += 5; if(Score % 10 == 0) {SpawnNewPizza();} if(Score % 6 == 0) {SpawnNewChips();} } } } } } public void Eaten(int amount) { sandstone sand = (sandstone) getWorld(); Counter theCounter = sand.getCounter(); theCounter.bumpScore(amount); }
danpost danpost

2014/5/25

#
First you need to use code tags around your code to display them properly in your discussion (see the 'Posting code?' link below the 'Post a reply' box). Next, how the score works is irrelevant to how the score is used at the end of a game. In other words, the methods given above are not what is needed in helping to get a high scores board working. Only how the 'Score' field is declared, where it is located and where you need it when the game ends.
danpost danpost

2014/5/25

#
@mjames3244, thanks, but not interested.
adi110 adi110

2014/5/25

#
so help me understand what i need to do in order to save the score in the leaderboard screen
adi110 adi110

2014/5/25

#
please help me, i really want to do a high score screen which saves me the best 5 scores
adi110 adi110

2014/5/25

#
here is my game ( http://www.greenfoot.org/scenarios/11556 ), if you can take a look and see it and tell me what to add to save the scores in my Leaderboard screen
danpost danpost

2014/5/26

#
adi110 wrote...
here is my game ( http://www.greenfoot.org/scenarios/11556 ), if you can take a look and see it and tell me what to add to save the scores in my Leaderboard screen
First things first -- your subclasses of Menus are not menus and should not subclass Menus. They should each extend World. Your Menus class should look like this:
import greenfoot.*;

public class Menu extends World
{
    public Menu()
    {    
        super(1024, 800, 1, true);
        addObject(new PlayButton(), 500, 250);
        addObject(new HelpButton(), 500, 400);
        addObject(new LeaderButton(), 500, 550);
        addObject(new CreditsButton(), 500, 700);
    }
}
and your other worlds that were subclasses of Menus should be similar to this:
import greenfoot.*;

public class HelpScreen extends World
{
    public HelpScreen()
    {
        super(1024, 800, 1, true);
        addObject(new BackButton(), 200, 700);
    }
}
Then you can create a LeaderBoard class to create the image showing the top five players with their scores and have the LeaderBoardScreen constructor create and add a LeaderBoard object into the world.
adi110 adi110

2014/5/26

#
if i change the Menus class and the subclasses of Menus nothing appear just my MenuScreen and the BackButton (no PlayButton, HelpButton, ScoreButton) please, if you can do what you said for me it will be great; i don't understand what to do...:(((
danpost danpost

2014/5/26

#
adi110 wrote...
if i change the Menus class and the subclasses of Menus nothing appear just my MenuScreen and the BackButton (no PlayButton, HelpButton, ScoreButton)
If you had change it to the way I said to change it, you certainly would be getting those buttons (lines 8 through 11 in my last post above adds those buttons to the world).
adi110 adi110

2014/5/26

#
i will try again and i will edit this post with my response : ok i've done it; now how do I save the scores at the end of a game (when i die) in the LeaderboardScreen (my top 5 scores and the names if i can)??
danpost danpost

2014/5/26

#
Actually, the scores will not be saved IN the LeaderboardScreen class. They are saved in a file located either on your system (when running greenfoot there) or on the server (when running the scenario on the site). There are two places in your project where the game can end (1) in the Lemur class and (2) in the Bomb class. In both these classes, just before setting a new MenuScreen world active, you will need to call a (new) method in your Sandstone class to deal with saving the score for that user if it is a new high score for that user if that user is logged on. The method would look like this (for the Sandstone class):
public void saveScore()
{
    if (UserInfo.isStorageAvailable()) // is user logged in and is server available
    {
        UserInfo me = UserInfo.getMyInfo(); // get user info
        if (getScore() > me.getScore()) // is new score higher than previous score
        {
            me.setScore(getScore()); // set new high score
            me.store(); // save updated info
        }
    }
}
The line to use to call this method would look like this:
theWorld.saveScore();
There are more replies on the next page.
1
2