I have an AS3 game that will be played on local desktops (not on network) and I need to save the players details after every game. I have tried a few methods (filestream, sharedObject, XML) but so far none have worked. The details to be saved are variables player_name and player_score. I just need these added to an existing file, they don't have to be sorted or filtered.
For this case I would use a simple LocalShardObject. I hope, you'll understand this code. Otherways, please ask. Greetings André
import flash.events.NetStatusEvent;
import flash.net.SharedObject;
var soName:String="yourSO"
function writeScore(playerName:String, playerScore:Number):void
{
var so:SharedObject = SharedObject.getLocal(soName);
// Write a new Object nemed by the playername in the so
so.data[playerName]= {name:PlayerName, playerScore:playerScore};
// add a successListener
so.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
// and flush it
so.flush();
}
function readSO():Object
{
var so:SharedObject; = SharedObject.getLocal(soName);
//If no lso was found
if (so.size === 0 || so.data.results === null) {
_results = [];
trace("lso not found");
return;
}
//For 'playerName' you can choose a name you want and add all your stuff to this object // it is basically an object
var playerName:String = so.data[playerName]name;
var playerScore:Number = so.data[playerName]playerScore;
trace(playerName, playerScore);
}
function onFlushStatus(event:NetStatusEvent):void
{
var so:SharedObject = event.target as SharedObject;
switch (event.info.code)
{
case "SharedObject.Flush.Success":
trace("value saved in the lso");
break;
case "SharedObject.Flush.Failed":
trace( "value not saved in the lso");
break;
}
so.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
}
//Let' try
writeScore("Andre", 10);
readSO("Andre");
writeScore("JayTray", 11);
readSO("JayTray");