Passing a string's variable by reference in Android - android

Hello all I'm new to android programming and I have a problem.
I created a sudoku app with preset puzzles for 3 difficulties using an e - book's directions.
The thing is that I made a puzzle generator for my puzzles to make them infinite.Now here is my problem :
To save your pre-defined puzzle in the book's example you had to use :
getPreferences(MODE_PRIVATE).edit().putString(PREF_PUZZLE,
toPuzzleString(puzzle)).commit();
toPuzzleString obviously converts the puzzle into a string(which was stored before in a 1 dimention array)
In order to load your saved settings to make the Continue option to work you had to use :
puz = getPreferences(MODE_PRIVATE).getString(PREF_PUZZLE,easyPuzzle);
BUT this works for a predefined puzzle stored in a private final string called "easypuzzle" at the beggining of the game.class . My puzzle is generated and stored in a 1 dimentional array when user clicks New Game.As a result I have to pass my generated puzzle as a reference(this is what I think) cause i tried to pass it like this :
getPreferences(MODE_PRIVATE).getString(PREF_PUZZLE,toPuzzleString(puzzle));
and when I close the game or just go back and try to Continue my game generates a new puzzle for me (or this is what I think it is) instead of loading the saved one.
What am I doing wrong? Can anyone help me , or tell me how to pass my puzzle as a reference like in c++?
Thank you all for your time any help would be appreciated....

As mentioned above, your puzzle may not be saved in preferences correctly. In order to check, change the getString(...) method to something like this:
String puzzleFromPrefs = getPreferences(MODE_PRIVATE).getString(PREF_PUZZLE, null);
if( null == puzzleFromPrefs ) {
// Then you know that it wasn't saved correctly...
}
This will cause null to be returned if the puzzle hasn't already been saved in the preferences. This will help narrow down where the issue may be.

Related

How to create image array in Kotlin?

I'm trying to an android version of an iOS app I recently launched. I'm struggling with image array. In iOS, add my image files, then call them by creating a var = UIImage(named:"name image file), then I use that.
In Android, I don't know how to access the drawable folder to make an image array that has multiple images, and how to call it in code so it is displayed in the image view on the screen.
I tried but had so many errors. Not many answers on the Net as well, been trying for two days.
For now, I am thinking of a button and an image view on the screen/activity. When I press the button, a random image appears. Could you please tell me how to go about it? (I took a Kotlin course on Treehouse but they only do an array of strings, I used that as a base but just don't know how to work with multiple images at all).
Appreciate your help, thanks.
used below code ..
var cards= arrayOf(R.drawable.card1,R.drawable.card2,R.drawable.card3,R.drawable.card4,R.drawable.card5,R.drawable.card6,R.drawable.card7);
button.setOnClickListener {
var r = new Random()
var n=r.nextInt(7)
imageview.setImageResource(cards[n]);
}

ParseObject.remove not working in Back4App

I am triying to delete a field of an object in Back4App, but I cannot achieve such a simple operation. By "deleting" I mean set a field that has data to "undefined".
According to the guide, I just have to call myObject.remove("field"). I tryed that (with correct field name), then save the object (I tried all of the saving functions available), but the object is unmodified. There is no error thrown.
I can change the field (with put ("field", otherObject), because it is a pointer field) with no problem. But put("field", JSONObject.NULL) is not working either.
I do not know if this code would work in the original Parse, I am coding this now. The equivalent function in iOS ([myObject removeObjectForKey:#"field"];) in the same database is working nicely...
For what I could gather from your question, the problem is that you're trying to clean a field from a relational object:
"I can change the field (with put ("field", otherObject), because it is
a pointer field)"
On that case, I'm not really sure if using simple object deletion would work. I'd suggest you to take a look at Parse's documentation on Relational Data in order to understand how you should remove that field.
Long story short, I'm not sure if the idea of cleaning the field that you wish will work, but what can be done when you have a relation like this:
ParseUser user = ParseUser.getCurrentUser();
ParseRelation<ParseObject> relation = user.getRelation("field");
relation.add(MyObject);
user.saveInBackground();
Is to simply remove the relation like this:
relation.remove(MyObject);
As you can check in the link above.

Retrieving preferences using Gdx.app.getPreferences

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()

AS3 Game, Collecting points, multiple movie clips with the same instance name, not working

I've decided to create an Android touch screen game. I am a complete and utter beginner and am learning as I go.
My game has a little elephant that moves up when you press and hold on the screen and falls when there is no contact with the screen. The aim is to collect as many peanuts that fly past as possible to gain the highest score. Pretty simple, you'd think so.
So far, I've managed to get to the point where the elephant can collide with a peanut and the peanut disappears.
My issue right now is, I can't create more than one peanut, with the same instance name of "peanut" because only the one will work and the others will not be recognized. I've done a good ole google search and nothing has really given me the right way to go. Could someone give me a clear answer of what to do or where to go from here?
If you need any more info, the code or a picture of what i've got so far to help you understand just let me know :)
Samantha
Instance name must be unique, and you cannot use instance name to find a set of movie clips. You should instead use an array, and at creating a peanut add it there too using say push(), and at collecting a peanut, splice it out.
In fact, whenever you get a multi-instance class with similar functionality (aka "collect"), use an Array to store references to all of these, so you will always know that ALL of your instances are accessible through that array.
How to work with arrays
A sample code:
var peanuts:Array=new Array();
function addPeanut(x:Number,y:Number):void {
var peanut:Peanut=new Peanut(); // make a peanut, you do this somewhere already
peanut.x=x;
peanut.y=y;
peanuts.push(peanut); // this is where the array plays its role
game.addChild(peanut); // let it be displayed. The "game" is whatever container
// you already have to contain all the peanuts.
}
function removePeanut(i:int):void {
// give it index in array, it's better than giving the peanut
var peanut:Peanut=peanuts[i]; // get array reference of that peanut
peanuts.splice(i,1); // remove the reference from the array by given index
game.removeChild(peanut); // and remove the actual peanut from display
}
function checkForPeanuts():void {
// call this every so often, at least once after all peanuts and player move
for (var i:int=peanuts.length-1; i>=0; i--) {
// going through all the peanuts in the array
var peanut:Peanut=peanuts[i];
if (player.hitTestObject(peanut)) {
// of course, get the proper reference of "player"!
// YAY got one of the peanuts!
// get some scoring done
// get special effects, if any
removePeanut(i); // remove the peanut
}
}
}

How to continue a Quiz game if it is stopped at any point in android

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.

Categories

Resources