I want to save the highscore created in the game and can be seen in the main menu when the player hits the highscore button, can someone help me?
you can use SQLITE to save the highscore to database another way is make a file that write the score to a Text File and save it to the directory of the system
You can solve your problem in a easier way..
Just declare a Variable for the Score like this..
local score=0
Then Increment the score variable by 1 whenever it hits the paddle. So Insert the coding in Collision Function as given below:
local function onCollision(event)
{
score=score+1
}
ball.collision=onCollision
ball:addEventListener("collision",ball)
Finally When you need to save your highscore (after gameover), You can use Preference instead of json which makes the larger coding.
local preference= require "preference"
local highscore=0
preference.save{highscore=score}
If you want to display the Highscore, then use the following:
highscore_value=preference.getValue("highscore")
display.newText(highscore_value,0,0,nil,30)
This might be useful for your problem !!
Multiple libraries exist for this purpose.
GGScore is an open source library built by GlitchGames which can easily allow you to do this: GlitchGames/GGScore
All the documentation you need is in the main page (README.md) so there isn't really a need for me to explain the code. But it's really easy to use.
Related
I am working on my application and I want to change the color of their UI with the click of button.....Like this....
Button change=findViewById(R.id.change_UI);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//My code to change the color of UI
//like blue to green-red-black etc (randomly)
}
The above things are working fine but when I again restart my application all things were same as I open it first time...with blue color UI.
I am new for development and i know this question is not so much interested but please help to solve this problem.
You should use SharedPreferences for this case. There is no problem for using it when you have more than 1 variable so don't worry.
Here is link to another answer how to use it properly: Android Shared preferences example
Another option could be creating one object which will hold all settings data and save it to a file and read it with every app run but for your case it would be overkill.
EDIT:
SharedPreferences data are stored in an XML file, a good practice would be not to store there more than 100kb. If you want to store something bigger you SQL database like Room or save yout data to file.
More information you can find here: Shared Preferences "limit" or in this answer https://stackoverflow.com/a/30638736/6329985
You can setup a preference activity as in here.
I have a simple question (may be very dumb), however I did not find an answer goggling around.
I'm trying to save a simple preference say "high score" on my game that I'm using libgdx to build.
Here is my sample code -
Preferences prefs2;
prefs2= Gdx.app.getPreferences("MyPreferences");
prefs2.putString("name", "Donald Duck");
String name = prefs2.getString("name", "No name stored");
font.draw(textBatcher, name, 55, 55);
Everything in the code (reading and writing to the xml file MyPreferences) works as expected except one thing; the high score stored on the file doesn't work when I restart the game. I definitely know that I'm missing something that is very simple but don't know what it is :)
Can any one please help me ?
I have also tried this -
Preferences prefs2;
if (prefs2 == null){
prefs2= Gdx.app.getPreferences("MyPreferences");
}
However it doesn't seem to work.
You're lacking a call to prefs2.flush() after you added the highscore item to the preferences object.
Straight from the libgdx docs:
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/Preferences.html#flush()
I am creating a multiple choice quiz game where there is an option to continue the game from where the user has left. Example, if user stopped the application at Question Number 4, then by pressing the continue option will resume the quiz game from Question Number 4 itself. I am not able to understand how should I move ahead by coding way? What is the right way to continue a multiple choice quiz game? Any help would be appreciated.
As an example, you could use JDOM. Just include the .jar file in your projects build path and then you'll have access to the JDOM library methods.
An example of using the JDOM libraries:
// Create a new XML document in memory
Document doc = new Document();
Element root = new Element("child");
doc.setRootElement(root); // set ^ above element as the root element
root.addContent(new Element( "childname" )); // add a data element to the root
root.getChild("childname").setText("SOME INFO"); // give some data to the element <childname>
// Save the XML file
FileWriter writer = new FileWriter("FILENAME.xml");
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat()); // sets correct tabbing/format in the file
outputter.output(doc, writer);
This would make something like:
<child>
<childname>SOME INFO</childname>
</child>
You can then access the information stored in the XML file by using some JDOM methods like:
root.getChild("SOME CHILD").getText();
Of course JDOM isn't the only option here. It's whatever seems easiest for you, not me.
Where are you getting your questions from? Try to build a sqlite db around your game. Get your questions from there. Then when the player goes out save state of the quiz in a db table, and when he comes back resume the game. Also you can store his previous performances, current performance in seperate tables. Seems like a good way to go about it.
Its a simple solution actually. A sql read will always result in the same sequence of read when you make a select call. So if you have 100 rows, and you select 10 rows of them and choose the first one, it will always be the same. Practically tested and it works. Now, you can create a "played" flag in your table and flag your quiz played. This way you will always get the same question in the same sequence as you reset.
If you want to randomize, I suggest you code that. There is no automatic way to do it.
Now for question, you should use shared preferences to save current question being played. This way you can just read that one row when you restart your application.
Hi I need a little help with an issue that I have.
I had to build an application which needs to show only a text information and pictures.The content was a lot, that's why instead of creating 200 single activities for very page I create one base activity which content I'm changing everytime depends on which listview item is clicked using putExtra(); and getExtra();. So the problem now is that they want me to create Favourites page, where the users can save some of the information and access them on a single activity.Actually it's really easy to do this using sqlite,but they want from me to finish the application today, within a few hours.My problem is that If i start coding it again and insert all that information in database it will take much more time for me.
So here is a little more explanation :
1.I have base activity with a listview.
2.When user click on listview item I send the content using putExtra in base activity.
So I need to learn how to save the id of listview item or something else and show that content in new Activity. I was thinking of using SharedPreferences but not really sure how to deal with that.
Any suggestions how I can do that...for a few hours.
Thanks in advance!
What kind of content is it? Is it over 200 entries and you don't save them locally?
Just knowing if it's a favorite or not can not be more than one hour job using sqlite. Just keep the id of the content and then a value to see if it's a favorite or not.
Noone will really do that for you since it's obviously your (paid?) job. The fact that you can't do it in time is really not a reason to skip the best option to use.
I would say don't be that optimistic about what you can achieve in which time. Manage your resources better and you will not have that problem.
You can achieve this with sharedpreferences too,but it's not a good idea.
The best way is to do it with database,but it's up to you.
I have a layout where users can add buttons and place them where they want.
I want to allow the user to save their layout so that it is loaded the next time they open the app.
Does anyone know if I can save the file on the sdcard? Alternatively I could use some kind of layout.getXml() method and put it in the database my app uses.
Thanks in advance
There is no file to save when you are generating a layout via code. You will have to create your own file format, which could be saved to the SD card or inserted into a database.
You can us savedInstaceState() method with the same type object as parameter .
there load the ui you want at the time of reloading.
In onCreate() method put a condition whethere that savedInstaceState obj is null or not . if not then call the LoadUI().
If I were you, I would create a class holding all the information about this layout and buttons. And write every information of the class to a file with JSONWriter. When the app has opened I just read the file and recreate the arrays using JSONObjects.