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

2018/1/14

High score System

1
2
3
Dimak Dimak

2018/1/14

#
Hey everybody. So I am working on a game similar to asteroids and I am showing a personal HighScore in the top right corner. Whenever I try to run the game outside of an account it crashes ( its because I call myInfo.getScore() I believe)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class HighScore extends Actor
{
    UserInfo myInfo = UserInfo.getMyInfo();
    int highScore = 0;
    int newScore = 0;
    public void HighScore()
    {
        if (UserInfo.isStorageAvailable())
        {
            highScore = myInfo.getScore();
        }
        draw();
    }   
 
    public void setHighScore(int newScore)
    {
        if (UserInfo.isStorageAvailable())
        {
            if (newScore > myInfo.getScore())
            {
                myInfo.setScore(newScore);
                myInfo.store();  // write back to server
                highScore = newScore;
            }
            else
            {
                highScore = myInfo.getScore();
            }
        }
        draw();
    }
 
    public void draw()
    {
        setImage(new GreenfootImage("HighScore : "+highScore,24,Color.WHITE,Color.BLACK));
    }
 
    public void act()
    {
        draw();
    }
}
The code works perfectly fine when a user is logged into an account. My question is how do I fix it so it does not crash when there is no account logged in? I am perfectly fine with it not keeping score of a non logged in user Thank you for your time
xbLank xbLank

2018/1/14

#
1
2
3
4
if(UserInfo.isStorageAvailable()) //checks if logged in
{
    //your code
}
Snippet of the API:
Dimak Dimak

2018/1/14

#
Where would I put that, and don't I already use it properly?
xbLank xbLank

2018/1/14

#
You call a
1
UserInfo myInfo = UserInfo.getMyInfo();
in line 4 without asking if the user is even logged in.
Dimak Dimak

2018/1/14

#
Oh thank you very much
Dimak Dimak

2018/1/14

#
Here is the new code, and for some reason it still doesn't work when incognito mode / not logged in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class HighScore extends Actor
{
 
    int highScore = 0;
    int newScore = 0;
 
    public void HighScore()
    {
        if (UserInfo.isStorageAvailable())
        {
            UserInfo myInfo = UserInfo.getMyInfo();
            setScore(myInfo.getScore());
        }
        draw();
    }   
 
    public void setScore(int i)
    {
        highScore = i;
    }
 
    public void setHighScore(int newScore)
    {
        if (UserInfo.isStorageAvailable())
        {
            UserInfo myInfo = UserInfo.getMyInfo();
 
            if (newScore > myInfo.getScore())
            {
                myInfo.setScore(newScore);
                myInfo.store();  // write back to server
                setScore(newScore);
            }
        }
        draw();
    }
 
    public void draw()
    {
        setImage(new GreenfootImage("HighScore : "+highScore,24,Color.WHITE,Color.BLACK));
    }
 
    public void act()
    {
        if (UserInfo.isStorageAvailable())
        {
            if (highScore == 0)
            {
                UserInfo myInfo = UserInfo.getMyInfo();
                setScore(myInfo.getScore());
            }
        }
        draw();
    }
}
xbLank xbLank

2018/1/14

#
What exactly is the error? Are you sure its caused by this (class)?
xbLank xbLank

2018/1/14

#
By the way: Same goes for you.
danpost danpost

2018/1/14

#
Here is what I percieve going down your code given above. (1) you have two fields of type 'int' -- the 'highScore' field, which is to hold the current high score; and the 'newScore' field, which is NOT used within the class (you can remove line 7); (2) you have a method called 'HighScore', which retrieves the UserInfo high score (if possible); but, the method is NOT called from anywhere; maybe you meant for it to be a constructor for the HighScore objects the class creates (hint was name of the method -- remove 'void' from line 9). (3) you have the two methods, 'setScore' and 'setHighScore'; I do not believe it necessary to have both as you probably call both at the same time and 'setHighScore' does do what 'setScore' does and more (remove the 'setScore' method and change line 34 to:
1
highScore = newScore;
(4) remove the act method; your text object has NO need to do anything on its own -- only when called to do something (it only needs redrawn when the high score changes); P.S. In general, I would have field for the UserInfo object. If null after object creation, no storage was available or user was not logged in. Checking the field for a null value is much quicker than checking to see if the database server is usable. In other words, you only have to check once when getting the info block and once when re-storing it.
xbLank xbLank

2018/1/14

#
It basically is just a copy pasted mess. Seriously. Why dont you just put some effort in a project.
Dimak Dimak

2018/1/14

#
(1) Fair enough (2) The HighScore Actor gets spawned in MyWorld at the very begging and is changed every time the player dies. Also isn't the HighScore() just a constructor for the actor, aka is executed whenever it spawns? (So now I remove void and it works, wow) (3) Okay did that (4) Yeah the only reason I had it there is so that it is drawn from the beggining and (2) fixes that too P.S. How would I check if UserInfo is null?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class HighScore extends Actor
{
    int highScore = 0;
    public HighScore()
    {
        if (UserInfo.isStorageAvailable())
        {
            UserInfo myInfo = UserInfo.getMyInfo();
             highScore = myInfo.getScore();
        }
        draw();
    }   
    public void setHighScore(int newScore)
    {
        if (UserInfo.isStorageAvailable())
        {
            UserInfo myInfo = UserInfo.getMyInfo();
            if (newScore > myInfo.getScore())
            {
                myInfo.setScore(newScore);
                myInfo.store();  // write back to server
                highScore = newScore;
            }
        }
        draw();
    }
    public void draw()
    {
        setImage(new GreenfootImage("HighScore : "+highScore,24,Color.WHITE,Color.BLACK));
    }
}
Dimak Dimak

2018/1/14

#
일리아스 wrote...
It basically is just a copy pasted mess. Seriously. Why dont you just put some effort in a project.
Two reasons : 1 I can't code in java and I have to do an assignment with coding 2 My teacher doesn't help me and therefore I ask the forums Sorry for wasting your time
xbLank xbLank

2018/1/14

#
I never said you are wasting my time. If anything then I waste my own time. My point is that you clearly do not understand your code. You yourself said you can't code. So why dont you start learning it? Don't blame your teacher. He might be shit. But in the end its up to you wheter you want to learn something or not. Instead of trying to fix a game you should try to fix your understanding of simple code. In the end you can't even be proud of your copy pasted mess. Set small goals and you will realize that it feels way better to create something your own.
Dimak Dimak

2018/1/14

#
I mean alright fair enough that I copy and paste stuff, but how else would you do a high score system otherwise? Also I just want to pass this course and move on to making my robot work, that's why I don't really care about this as much.
xbLank xbLank

2018/1/14

#
If you dont even care how can you expect us to care? Besides I checked that guys project and I can see that you literally copy pasted his HighScore class. Its not about doing something different. Its about doing it yourself. Putting a thought or two in something. Anyways. Tell the teacher your thoughts and enjoy building your robot. I refuse to help you, sorry.
There are more replies on the next page.
1
2
3