Highscore

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

Highscore

Postby Kup » Fri Nov 09, 2007 3:38 pm

Hi

i've got this piece of code from the nokia forum on how to make a highscore. http://wiki.forum.nokia.com/index.php/How_to_create_a_high_score_database_in_Java_ME
but when i try to read the content it doesnt work.
i've changed it a bit because it had a few erros, but it still doens't work, can anyone help please?
RecordStore highscore;
RecordEnumeration enume;
byte[] record;
byte[] strb;
String str;
int id;
int fin;

try{
highscore = RecordStore.openRecordStore("Pontuacao", true);

str = new String("John,3400");
strb = str.getBytes();
highscore.addRecord(strb, 0, strb.length);

enume = highscore.enumerateRecords(null, null, false);
while (enume.hasNextElement()){
id = enume.nextRecordId();
fin = enume.getRecord(id, record, 0);
str = new String(fin);
}

}catch(IOException ex){
ex.printStackTrace();
}finally{
highscore.closeRecordStore();
}


thanks ;)
Kup
 
Posts: 32
Joined: Wed Sep 26, 2007 3:35 pm
Location: Portugal

Re: Highscore

Postby Kup » Mon Nov 12, 2007 2:32 pm

the problem here is in the getRecord(); but i cant see why

any thoughts?
Kup
 
Posts: 32
Joined: Wed Sep 26, 2007 3:35 pm
Location: Portugal

Re: Highscore

Postby DevelopmentTeam » Mon Nov 12, 2007 2:55 pm

The code in the nokia forum is working fine. And I dont see any problem in their code.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Highscore

Postby Kup » Mon Nov 12, 2007 3:07 pm

really :oops: ? mine is giving me a whole bunch of errors since bad variables (like Int and bytes) to key words(enum).

i've managed to get the getRecord running (i was calling enume instead of highscore) but now i cant make the transition to the string, what do you suggest?
Kup
 
Posts: 32
Joined: Wed Sep 26, 2007 3:35 pm
Location: Portugal

Re: Highscore

Postby Kup » Mon Nov 12, 2007 3:16 pm

oh and also i've read in the java sun site that the two kinds of getRecord
getRecord(int);
getRecord(int, byte[], int);
Use the second form of getRecord whenever possible, this form uses less memory than the first form, and does not overextend the garbage collector

http://java.sun.com/developer/J2METechTips/2001/tt0220.html
the second spends less memory, and since that last time you warned me to be carefull with the memory :) i was trying to adapt the code a little bit so it wouldnt be so heavy
Kup
 
Posts: 32
Joined: Wed Sep 26, 2007 3:35 pm
Location: Portugal

Re: Highscore

Postby Kup » Mon Nov 12, 2007 4:49 pm

RecordStore highscore = null;
RecordEnumeration enume;
byte[] record = null;
byte[] strb = null;
String str;
int id;
int fin;

try{
highscore = RecordStore.openRecordStore("Pontuacao", true);
str = new String("John,3400");
strb = str.getBytes();
highscore.addRecord(strb, 0, strb.length);

enume = highscore.enumerateRecords(null, null, false);
while (enume.hasNextElement()){
id = enume.nextRecordId();
highscore.getRecord(id, record, 0);
str = new String(record);
System.out.println(str);
}
}catch( RecordStoreException ei){
ei.printStackTrace();
}finally{
highscore.closeRecordStore();
}

ok i was loading the int in the string and not the byte :? my bad :oops:

but even now it doesnt give me any error in the compilation it just gives me a null poiter exeption
Kup
 
Posts: 32
Joined: Wed Sep 26, 2007 3:35 pm
Location: Portugal

Re: Highscore

Postby DevelopmentTeam » Tue Nov 13, 2007 2:07 am

when you assign an int to a String the number is converted to ascii and stored in the String. You cannot expect an error.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Highscore

Postby Kup » Wed Nov 14, 2007 6:24 pm

ok finally got it running !
RecordStore highscore = null;
RecordEnumeration enume;
byte[] record = null;
byte[] strb = null;
String str;
int id;
int fin;

highscore = RecordStore.openRecordStore("Pontuacao", true);
//to add
str = new String("Jo
Kup
 
Posts: 32
Joined: Wed Sep 26, 2007 3:35 pm
Location: Portugal

Re: Highscore

Postby DevelopmentTeam » Thu Nov 15, 2007 3:05 am

congrats and have a nice day...
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Highscore

Postby Kup » Thu Nov 15, 2007 12:18 pm

:D well not actually a nice day because now i've got to order the records

any idea on how to make this without having to create two record stores?



thanks :wink:
Kup
 
Posts: 32
Joined: Wed Sep 26, 2007 3:35 pm
Location: Portugal

Re: Highscore

Postby DevelopmentTeam » Thu Nov 15, 2007 1:56 pm

In such case what we do is read all the records in to an array and shuffle it as we like (ascending or descending), then delete the records and then create a fresh recordset.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Highscore

Postby Kup » Thu Nov 15, 2007 3:52 pm

nice

thanks again :wink:
Kup
 
Posts: 32
Joined: Wed Sep 26, 2007 3:35 pm
Location: Portugal

Re: Highscore

Postby DevelopmentTeam » Fri Nov 16, 2007 2:20 am

hope to be a nice day this time. :)
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: Bing [Bot] and 7 guests

cron