-
-
Save PrimaryFeather/87a9b33b989a26476e93845fa631a5f0 to your computer and use it in GitHub Desktop.
Simple test to showcase login & savegame logic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================================================================================================= | |
// | |
// Starling Framework | |
// Copyright Gamua GmbH. All Rights Reserved. | |
// | |
// This program is free software. You can redistribute and/or modify it | |
// in accordance with the terms of the accompanying license agreement. | |
// | |
// ================================================================================================= | |
package | |
{ | |
import com.gamua.flox.Entity; | |
import com.gamua.flox.Flox; | |
import com.gamua.flox.Player; | |
import com.gamua.flox.utils.HttpStatus; | |
import flash.display.Sprite; | |
public class FloxLoginText extends Sprite | |
{ | |
public function FloxLoginText() | |
{ | |
Flox.playerClass = CustomPlayer; | |
Flox.init("GAME ID", "GAME KEY"); | |
var gameCenterID:String = "g:1234567"; | |
var playerAlias:String = "mike"; | |
var player:CustomPlayer = Player.current as CustomPlayer; | |
if (player != null && player.gameCenterID == gameCenterID) | |
{ | |
// the correct player is already logged in! | |
loadSaveGame(); | |
} | |
else | |
{ | |
Player.loginWithKey(gameCenterID, | |
function onComplete(player:CustomPlayer):void | |
{ | |
player.alias = playerAlias; | |
player.gameCenterID = gameCenterID; | |
loadSaveGame(); | |
}, | |
function onError(error:String):void | |
{ | |
trace('Error: on first start, player must be online.'); | |
}); | |
} | |
} | |
private function loadSaveGame():void | |
{ | |
var saveGameID:String = Player.current.id; | |
Entity.load(SaveGame, saveGameID, | |
function onComplete(saveGame:SaveGame):void | |
{ | |
saveGame.score += 1; // just to test if data is stored. | |
onSaveGameLoaded(saveGame); | |
saveGame.saveQueued(); | |
}, | |
function onError(error:String, httpStatus:int, cache:SaveGame):void | |
{ | |
if (cache) | |
{ | |
// device probably offline, but we still have a save game to use. | |
onSaveGameLoaded(cache); | |
} | |
else if (httpStatus == HttpStatus.NOT_FOUND) | |
{ | |
// player has logged in the first time | |
var saveGame:SaveGame = new SaveGame(); | |
saveGame.id = saveGameID; | |
saveGame.score = 1; | |
saveGame.saveQueued(); | |
onSaveGameLoaded(saveGame); | |
} | |
else | |
{ | |
trace("User must try again when online!"); | |
} | |
}); | |
} | |
private function onSaveGameLoaded(saveGame:SaveGame):void | |
{ | |
trace("loaded SaveGame! Score:", saveGame.score); | |
} | |
} | |
} | |
import com.gamua.flox.Entity; | |
import com.gamua.flox.Player; | |
class CustomPlayer extends Player | |
{ | |
private var _alias:String; | |
private var _gameCenterID:String; | |
public function get alias():String { return _alias; } | |
public function set alias(value:String):void { _alias = value; } | |
public function get gameCenterID():String { return _gameCenterID; } | |
public function set gameCenterID(value:String):void { _gameCenterID = value; } | |
} | |
class SaveGame extends Entity | |
{ | |
private var _score:int; | |
public function get score():int { return _score; } | |
public function set score(value:int):void { _score = value; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment