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

2014/2/27

getMyInfo().setInt(int index, int value) Problem

Pointifix Pointifix

2014/2/27

#
Hi guys, me annoying again ;) My problem is that this code does not increase the int value of a given index, its in a button class that should get covered green if its clicked, the more often it is clicked, the more green the button should get!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(index!=0)
        {
            getImage().setColor(Color.green);
            getImage().fillRect(0,0,(getImage().getWidth()/10)*UserInfo.getMyInfo().getInt(index),height);
             
            getImage().setColor(new Color(155,0,0));
            getImage().drawString("Cost: "+UserInfo.getMyInfo().getInt(index)*10,getImage().getWidth()/2-fm.stringWidth("Cost: "+UserInfo.getMyInfo().getInt(index)*10)/2,height/2+fm.getHeight()/4);
 
            if(Clicked())
            {
                UserInfo.getMyInfo().setInt(index,UserInfo.getMyInfo().getInt(index)+1);
                UserInfo.getMyInfo().store();
            }
        }
The code of Clicked just simply:
1
2
3
4
5
public boolean Clicked()
    {
        if(Greenfoot.mouseClicked(this))return true;
        else return false;
    }
Thanks for help again!
danpost danpost

2014/2/27

#
I think you need to create a local field to hold the UserInfo object, change it, then save it:
1
2
3
4
// to replace lines 11 and 12
UserInfo ui = UserInfo.getMyInfo();
ui.setInt(index, ui.getInt(index)+1);
ui.store();
Also, you must make sure that the user is logged in before doing the above, by using:
1
if (UserInfo.isStorageAvailable())
Pointifix Pointifix

2014/2/27

#
uih you are right i had to store it local in a UserInfo variable, thanks again danpost ;) by the way what does getMyInfo() return if the user is not logged in?
danpost danpost

2014/2/27

#
Pointifix wrote...
...by the way what does getMyInfo() return if the user is not logged in?
That is something you could test and figure out yourself:
1
System.out.println(""+UserInfo.getMyInfo());
Before running, go to 'Edit>Preferences.." on the menubar and clear the username field (Player name) at the bottom of the Miscellaneous tab.
You need to login to post a reply.