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

2012/7/4

userinfo tutorial

1
2
steved steved

2012/7/4

#
does anyone know of a good tutorial for user info? There is only so much I can figure out from the javadoc and i would really like to learn it.
danpost danpost

2012/7/4

#
There really is not too much to learn about it. What are you having problems with?
SPower SPower

2012/7/4

#
http://www.greenfoot.org/scenarios/4981 One of my tutorials. From now on, if you ever need help for something, you first look here, ok?: http://www.greenfoot.org/collections/321 This is just a collection of some tutorials, and it's growing (slowly :) ).
steved steved

2012/7/4

#
Can you store a bunch of variables per user so that you can load them later? Like so you can tell which levels a user has unlocked and such
danpost danpost

2012/7/4

#
You are allowed 10 integer values and 5 strings up to 50 characters in length, per user, per scenario. It is not a 'bunch' of variables, but you may be able to condense what info you need stored to fit.
davmac davmac

2012/7/5

#
Use setInt(...), setScore(...) and setString(...) methods to set various values and then call store() to save the data. You need to decide your own meaning for the various ints/strings. For instance, you could use one of the Strings to keep track of which levels have been unlocked - have your string be a series of characters like "xxxxoooxxxoo" where "x" means locked and "o" means unlocked, for example.
steved steved

2012/7/11

#
Thanks guys for the help. How do you check individual characters of a string? Once I learn that I can get things working.
danpost danpost

2012/7/11

#
I will call the string 'xoString'.
for (int index = 0; index < xoString.length(); index++)
{
    if ('x' == xoString.charAt(index))
    {
        // process for an 'x' character at index
    }
    else if ('o' == xoString.charAt(index))
    {
        // process for an 'o' character at index
    }
}
where if index is 0, we are looking at the first character, if index is 1, we are looking at the second, etc. Of course, if the only 2 possibilities for each character is an 'x' or an 'o', then the 'if' part after 'else' can be dropped (but, does not hurt to have it there, either). You probably could have gone to the Java tutorials (or trails) and found this out yourself.
danpost danpost

2012/7/11

#
BTW, how many levels do you currently have for this scenario?
steved steved

2012/7/12

#
I have 20 levels, that can be catagorized in 4 sets of 5 (I have 4 zones). What I will probably do is let you be able to unlock them one zone at a time so once you beat a zone you can play all of its levels later.
danpost danpost

2012/7/12

#
OK, so that will only take less than half of one String variable in UserInfo, leaving plenty of space for whatever. If you are in a crunch for storage, however, it is possible to store that information (the 20 'x's and 'o's) differently, taking only 3 characters of a string (actually, only 2 and a half).
steved steved

2012/7/12

#
How could I do that, and how do you use 2 and a half characters?
danpost danpost

2012/7/12

#
I really do not know if you want to actually go this route. But... Each byte consists of eight bits, each of which can be on or off (an 'x' or an 'o'). Since you have 20 levels, two bytes (16 bits) and a half a byte (4 bits) would be 20 bits, one for each level. The code for setting up the data using bytes would be more tedious than using an int, which is shown below (an int variable uses 4 bytes ; which is plenty).
String xoString = "xooxxoxxxooxxooxooox";
int levelInt = codeLevelInt(xoString);
String xoStr = decodeLevelInt(levelInt);
System.out.println("The output matches the input:  " + xoString.equals(xoStr));
//  the above is background
private int codeLevelInt(String xoStr)
{
    int levInt = 0;
    for (int lev = 0; lev < xoStr.length(); lev++)
    {
        levInt  = levInt * 2;
        levInt = levInt + ('x' == xoStr.charAt(xoStr.length() - 1 - lev) ? 1 : 0);
    }
    return levInt;
}

private String decodeLevelInt(int levInt)
{
    String xoStr = "";
    while (xoStr.length() < 20)
    {
        xoStr = xoStr + (levInt % 2 == 1 ? "x" : "o");
        levInt = levInt / 2;
    }
    return xoStr;
}
Even though an int is a 32-bit variable, this is still only using 20 bits within it.
steved steved

2012/7/13

#
I think I will probably stick to the pevious method as I doubt I will really ever need to save space. It's unlikely I will ever need to use more then 50 characters, even if I do it would be simpler to use 2 strings I think :D.
erdelf erdelf

2012/7/13

#
why not simply use an int with the level the user reached.
There are more replies on the next page.
1
2