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

2022/5/22

achievements

elvacan elvacan

2022/5/22

#
Hello, I'm new to this and I wanted you to tell me how to make a score record, that is, when the player loses, a list of his current score is saved and then when he exceeds it, a new score appears
elvacan elvacan

2022/5/23

#
hello, i need your answers
elvacan elvacan

2022/5/23

#
please
RcCookie RcCookie

2022/5/23

#
You can use the UserInfo class:
if(!UserInfo.isStorageAvailable()) {
    System.err.println("Cannot save score!");
    return -1;
}
UserInfo user = UserInfo.getMyInfo();
if(user.getScore() >= score)
    return user.getScore();
user.setScore(score);
user.store();
return score;
This takes the variable „score“ and updates the score field in UserInfo. It returns the highscore.
elvacan elvacan

2022/5/24

#
the example gives me an error but what I mean is that when the player loses a list is saved with their current score and is updated when they get points and also the points system is simple the one I have
elvacan elvacan

2022/5/24

#
and if you could pass me the example done I would appreciate it
RcCookie RcCookie

2022/5/24

#
Why do you want to save a list? And what error do yo get? You need to put that code into a method, for example public int updateScore(int score).
elvacan elvacan

2022/5/24

#
si lo quiero guardar en una lista
RcCookie RcCookie

2022/5/25

#
(That doesn't make answering a lot easiert)
RcCookie RcCookie

2022/5/25

#
In the UserInfo class, you have limited amounts of storage available: 1x int "score" 10x int "int" 5x String "string", max length 50 chars If your list is never longer than 10 items, you can just save entry by entry in the "int" fields. However, you should consider what exactly you need to save. Remember the only place you will need the stored values is in these cases: - Compare whether the new score is greater than the saved score - Show the high score If you use a list during the calculation while the game is running, that is fine, but you should then try to reduce the list into a single value which, the higher it is, indicates a better score. That is easier to compare, easier to store, and easier to display.
RcCookie RcCookie

2022/5/25

#
But if you want to do that first approach of saving an array into UserInfo, this is a way to do it:
public void storeArray(int[] array) {
    UserInfo user = UserInfo.getMyInfo();
    for(int i=0; i<array.length; i++)
        user.setInt(array[i]);
    user.store();
This does, however, not compare the values to the previous score (because I don't know how they should be compared). It would really help if you would explain why you think you need a list, and what each entry of that list means.
danpost danpost

2022/5/25

#
There is example code given within the documentation of the UserInfo class that should be sufficient for your needs (as far as saving new high scores).
RcCookie wrote...
for(int i=0; i<array.length; i++)
    user.setInt(array[i]);
    user.store();
The setInt method requires 2 parameters. Line 2 should be:
user.setInt(i, array[i]);
where the variable i, or the length of the array, cannot be greater than 10.
elvacan elvacan

2022/5/26

#
ok thank you all :)
RcCookie RcCookie

2022/5/26

#
RcCookie wrote...
The setInt method requires 2 parameters.
Oh yeah you're right about that, sorry
danpost danpost

2022/5/26

#
RcCookie wrote...
RcCookie wrote...
Should be:
danpost wrote...
You need to login to post a reply.