This is for the game i am doing When I click the Home button on the android I want it to save the frame it was in and not start the game loads where it was
This results in saving the game forever but I just want it to save once and the code to load does not work
import flash.net.SharedObject;
stage.addEventListener(KeyboardEvent.KEY_DOWN, buttonPressed);
var stateSO:SharedObject = SharedObject.getLocal("saveState");
if (stateSO.size > 0 && stateSO.data.lastFrame && stateSO.data.lastFrame != undefined) {
//the shared object exists and has a valid value, so lets skip to that frame
gotoAndPlay(stateSO.data.lastFrame);
trace("Load Here.");
}
function buttonPressed(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.MENU){
//this function runs every frame
function saveState(Event){
stateSO.flush(); //save the cookie (shared object)
trace("Save Here.");
return;
}
addEventListener(Event.ENTER_FRAME, saveState);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, buttonPressed);
}
}
thanks for help
There's unnecessary code inside your keypress event handler (and other places), and you never actually write the value you want to save. Also, always format your code because a lot of errors could occur just because you cannot read clearly what your code is actually doing.
import flash.net.SharedObject;
var stateSO:SharedObject;
// I guess this is a frame script, lets run it
// only once to avoid the possible mess.
if (!stateSO)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, buttonPressed);
stateSO = SharedObject.getLocal("saveState");
if (stateSO.data.lastFrame != undefined)
{
trace("Load Here.");
gotoAndPlay(stateSO.data.lastFrame);
}
}
function buttonPressed(e:KeyboardEvent):void
{
// I hope you know what this button is.
if (e.keyCode == Keyboard.MENU)
{
saveState();
}
}
function saveState():void
{
// Write current frame to the SharedObject.
stateSO.data.lastFrame = currentFrame;
stateSO.flush();
trace("Save Here.");
}