When i have tested my game on desktop the prefs work perfectly and correct scores and highscore is output but when i run it on mobile the values do not save and for both scores show 0. There is no errors also. Just need some help to try and fix this. Thanks alex.
pref = Gdx.app.getPreferences("Scores");
if(pref.contains("Score") == false) {
pref.putInteger("Score", 0);
if(pref.contains("HighScore") == false){
pref.putInteger("HighScore", 0);
}
}
GameOver Screen
pref = Gdx.app.getPreferences("Scores");
score = pref.getInteger("Score");
highScore = pref.getInteger("HighScore");
if(score > highScore){
pref.putInteger("HighScore", score);
highScore = score;
}
This is ran when the player dies.
public void updateScore(){
if (pref.contains("Score") == true){
}
pref.putInteger("Score", hud.getScore());
}
You need to call flush() method after putting a value to preferences - it causes commiting data to the memory
pref.putInteger("Score", hud.getScore()).flush();
The a look at official wiki also
Related
I'm been searching for a way to check the Siri Remote's current orientation or to register for Siri Remote orientation changes, but I haven't found anything yet. Is it possible to do this (without resorting to interpreting the raw gravity data)?
I've have found out how to disable auto-orientation changes with "allowsRotation" on "microGamepad". Which is pretty cool!
I didn't see an API either, however as you mentioned, you can poll for the gravity data, and I just wanted to post that code here in case some find it useful. You can modify this to your own need, such as detecting last orientation and comparing it to current if you want a callback of the change.
//**************************************************************
// Update loop portion to UIViewController
//**************************************************************
#property (strong) CADisplayLink *updateLoopTimer;
self.updateLoopTimer = [CADisplayLink displayLinkWithTarget:self selector:#selector(updateRefreshRate:)];
[self.updateLoopTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
-(void)updateRefreshRate:(CADisplayLink *)displayLink
{
CFTimeInterval deltaTime = displayLink.duration * displayLink.frameInterval;
[self update:(float)deltaTime];
}
//******************************************************
// Update loop
//******************************************************
-(void)update:(float)dt
{
#ifdef TV
//***************************************
// Detect button presses
//***************************************
//self.gameControler = [[GCController controllers] firstObject];
if( self.gameController != nil )
{
GCMicroGamepad* microPad = self.gameController.microGamepad;
if ( microPad != nil )
{
GCMotion *motion = self.gameController.motion;
GCControllerDirectionPad *dpad = microPad.dpad;
if( motion != nil )
{
GCAcceleration accelVector = motion.gravity;
if( fabs(accelVector.x) > fabs(accelVector.y) )
{
NSLog(#"Sideways");
}
else
{
NSLog(#"Upright");
}
}
}
}
#endif
}
Hey everyone so basically what I am trying to accomplish is saving a number with the Shared Object which the the coins that the player collects in the game and if they player quits out of the game and comes back to play again the amount of coins he had when he quit will still show and he will be able to add to that amount if the player picks up more coins. I had this working for a bit but then the textfield started displaying "NAN".
Here is how I have it set up.
The Variable private var nCoins:Number;
In the constructor function I have:
sharedObjectCoins = SharedObject.getLocal("CoinsData");
nCoins = 0 + sharedObjectCoins.data.tCoins;
if (sharedObjectCoins.data.tCoins == null)
{
sharedObjectCoins.data.tCoins = nCoins;
}else
{
trace("Save data found."); // if we did find data...
loadDataTimeAttack(); // ...load the data
}
and in the games Enter.Frame Loop I have the function saveDataCoins which is setup like so:
private function saveDataCoins():void
{
if (nCoins > sharedObjectCoins.data.tCoins )
{
sharedObjectCoins.data.tCoins = nCoins;
}
coinsGraphic.coinsText.text = " " + sharedObjectCoins.data.tCoins;
sharedObjectCoins.flush();
}
not sure if you need the function to where the hitTest takes place between the coins and player but here it is:
private function checkPlayerHitCoins():void
{
for (var i:int = 0; i < aCoinsArray.length; i++)
{
//get current point in i loop
var currentCoins:mcCoin = aCoinsArray[i];
//test if player is hitting current point
if(player.hitTestObject(currentCoins))
{
nCoins += 1;
updatecoinsTextScore();
updateCoinsPauseScreen();
//Add points sound effects
var coinsSEffect:Sound = new coinsSound();
coinsSEffect.play();
//remove point on stage
currentCoins.destroyCoins();
//remove points from array
aCoinsArray.splice(i, 1);
trace("Hit: " + aCoinsArray.length);
}
}
}
Please if anyone could help me with this maybe point something out that I am doing wrong. This code worked perfect one time and when I closed the screen and came back to re test it the textfield displayed NAN and thats it when I hitTest the coins sometimes the NAN switches to a number for like a second but then goes back to NAN.
The first time (or rather every time it creates a new shared object) you will be trying to add undefined to 0, which will result in either a runtime error or NaN.
You need to check if the value exists before attempting to do addition with it.
if(sharedObjectCoints.data && sharedObjectCoins.data.tCoins && !isNaN(sharedObjectCoins.data.tCoins)){
nCoins = Number(sharedObjectCoins.data.tCoins); //there's not point in adding 0
trace("Save data found."); // if we did find data...
loadDataTimeAttack(); // ...load the data
}else{
sharedObjectCoins.data.tCoins = nCoins;
}
Also, if you don't manually set a value to a number var, it will start off life as NaN. eg var nCoins:Number will be NaN until you set it to something.
That said, working with the sharedObject directly like this is a very sloppy way to code your program. Really you should just use shared object to load and save the value, and everything in between use a strongly typed variable.
var nCoins:int = 0;
var tCoins:int = 0;
sharedObjectCoins = SharedObject.getLocal("CoinsData");
if(sharedObjectCoins.data && sharedObjectCoins.data.tCoins && !isNaN(sharedObjectCoins.data.tCoins){
tCoins = int(sharedObjectCoins.data.tCoins);
}else{
//no shared object, use default value for tCoins
tCoins = 0; //or whatever it should start off as.
}
Then write a save function
private function saveSharedObject():void {
sharedObjectCoins.data.tCoins = tCoins;
sharedObjectCoins.flush();
}
Then replace all other instances of sharedObjectCoins.data.tCoins with the var tCoins
It's probably best not to flush the shared object every frame for performance purposes.
Also, shared objects may or may not actually save, depending on user preferences, storage space available, etc. They should not be relied upon for critical data retention.
You can listen for problems with the shared object with AsyncErrorEvent.ASYNC_ERROR I believe (It's been a while since I've worked with AS3 Shared Objects)
I'm trying to test some strings but trace outputs nothing. I was using Flash CS6, then I installed Flash CC then everything was fine. But then I created another loop now the same thing. Here's my code:
for (tempNum = lastCharNum; tempNum <= 7; tempNum++) {
trace(tempNum); // this outputs nothing
if (arr[tempNum] != String.fromCharCode(9)) {
firstCharNum = tempNum;
trace(firstCharNum); // this outputs nothing
}
}
I tried:
for (tempNum = lastCharNum; tempNum <= 7; tempNum++) {
txt.text = String(tempNum); // this doesn't make change to the textfield
if (arr[tempNum] != String.fromCharCode(9)) {
firstCharNum = tempNum;
txt.text = String(firstCharNum); // this doesn't make change to the textfield
}
}
Same thing, nothing happens.
Make sure "Omit Trace Statements" is not checked in the the publish settings. If its not checked, try File>Publish and Control>Test Movie>In Flash Professional to see if you get different results.
I'm working on a match-3 style puzzle game using Flixel, and so I'm working on checking each row and column to see if there is a match at any given time. However, I have 6 different pieces (as of right now) that are active, and each piece is identified by an integer. Given that, I can check, for each and every single piece, by doing something like this:
public function matchingCheck():void
{
if (piecesArray[0][1] == 1 && piecesArray[1][1] == 1 && piecesArray[2][1] == 1) {
FlxG.log("Yay!");
}
}
However, this is rather unwieldy and would basically cause way too much repetition for my liking.
At the very least, I would like to be able to check if the values in these arrays are equal to one another, without having to specify which value it is. At the very best, I'd love to be able to check an entire row for three (or more) adjacent pieces, but I will settle for doing that part manually.
Thanks for your help!
EDIT: Nevermind, my edit didn't work. It was just checking if piecesArray[2][1] == 1, which makes me a sad panda.
EDIT 2: I've selected the correct answer below - it's not exactly what I used, but it definitely got me started. Thanks Apocalyptic0n3!
You could cut down on that code a little bit by using another function
private function checkValid( arrayOfItemsToCheck:Array, value:* ):Boolean {
for ( var i:Number = 0; i < arrayOfItemsToCheck.length; i++ ) {
if ( arrayOfItemsToCheck[i] != value ) {
return false;
}
}
return true;
}
Then you just do this in your if statement:
if ( checkValid( [ piecesArray[0][1], piecesArray[1][1], piecesArray[2][1] ], 1 ) ) {
FlxG.log("Yay!");
}
That does assume all items need to be equal to 1, though. It's still a lot of code, but it cuts out one set of "= 1 &&" for each check.
How about something like this which would tell you both if a match existed and what match it was:
public function checkForMatch():void{
var rows:int = piecesArray.length;
for(var i:int=0; i<rows; i++){
var match:int = checkRow(piecesArray[i]);
if(match > -1) {
FlxG.log("Yay you matched " + match);
}
}
}
private function ckeckRow(row:Array):int{
if(row[0] == row[1] == row[2]){
return row[0];
}
return -1;
}
You know how vBulletin has a sql profiler when in debug mode? How would I go about building one for my own web application? It's built in procedural PHP.
Thanks.
http://dev.mysql.com/tech-resources/articles/using-new-query-profiler.html
The above link links how you can get al the sql profile information after any query.
Best way to implement it is to create a database class and have it have a "profile" flag to turn on logging of queries and the appropriate information as shown int he link above.
Example:
Class dbthing{
var $profile = false;
function __construct($profile = false){
if($profile){
$this->query('set profiling=1');
$this->profile = true;
}
...
}
function query($sql, $profile_this == false){
...
if($this->profile && !$profile_this)
$this->query("select sum(duration) as qtime from information_schema.profiling where query_id=1", true);
... // store the timing here
}
}
I use a database connection wrapper that I can place a profiling wrapper arround. This way I can discard the wrapper, or change it, without changing my base connector class.
class dbcon {
function query( $q ) {}
}
class profiled_dbcon()
{
private $dbcon;
private $thresh;
function __construct( dbcon $d, $thresh=false )
{
$this->dbcon = $d;
$this->thresh = $thresh;
}
function queury( $q )
{
$begin = microtime( true );
$result = this->dbcon->query();
$end = microtime( true );
if( $this->thresh && ($end - $begin) >= $this->thresh ) error_log( ... );
return $result;
}
}
For profiling with a 10 second threshold:
$dbc = new profiled_dbcon( new dbcon(), 10 );
I have it use error_log() what the times were. I would not log query performance back to the database server, that affects the database server performance. You'd rather have your web-heads absorb that impact.
Though late, Open PHP MyProfiler would help you achieve this, and you can extract functional sections from the code for your usage.