View Full Version : save/load game button using cookies?
ThePearl
06-03-2008, 07:40 PM
Hello there
How to you manage to save/load the game using cookies? I want it to be like... I got 3 buttons, and 1 is "unlocked", and 2 "locked". SO, when I complete level 1, then level 2 button is "unlocked" and when I complete level 2, the level 3 button is "unlocked"?
Then when they are "unlocked" I can just klick them to go to that level.
xxneon
06-03-2008, 08:04 PM
check out SharedObject in flash help or google it .. SharedObject is flashes own version of a cookie file.. you can store data to the shared object .. and then retreive the info the next time the swf is opened by the user.
ThePearl
06-04-2008, 04:04 PM
Yeah I did... But I didn't really get it :/, I'll read it again now when I'm not as tired
Potemkyn
06-04-2008, 06:06 PM
You could pass the variables to JavaScript via ExternalInterface.call("passToCookie", lockVariable) in your Actionscript. (Thanks xxneon)
In the JavaScript in your HTML page (that loaded your swf), you would have a script to create the cookie when the page loads, and also the function passToCookie(lockVariable) that sets the variable in the cookie using thisMovie.SetVariable("lastLockVariableStatus" lastLockCookieStatus).
When you want to see what the lock was last, you'll need an ActionScript function ExternalInterface.call("checkLockVariable") that calls JavaScript to get the value out of the cookie and passes it up to Flash.
Flash would then need a watch() function that monitors a particular variable that JavaScript will change. On that change, the watch calls a function to set the lastLockVariableStatus to see what was set to last.
Aside from the JavaScript cookie part, I've found the EI call to JavaScript works fine, as well as JavaScript setting a variable in Flash with Flash watching and acting upon the change.
Mike
xxneon
06-04-2008, 06:07 PM
just so there is no confusing.. he is refering to ExternalInterface.
Potemkyn
06-04-2008, 06:10 PM
just so there is no confusing.. he is refering to ExternalInterface.
Thanks... I was just editing when I saw your post
Mike
xxneon
06-04-2008, 06:17 PM
I would say SharedObjecy would be best if the data is small.. if it gets pretty sizable then use ExternalInterface with javascript.
Shared Object is alittle bit difficult at first but after you attempt it a few times and get it to work its really simple to setup.
ThePearl
06-04-2008, 08:34 PM
I managed to get this cookie script... But how do I make it to save the level(frame) instead of the text?
Is it possible to store the level, and give it the name of level2, then you type in level2 in a textbox and it'll go to that frame? It should be, but I don't know how...
I really appreciate any help on this one!
http://dump.no/files/c33e5f5f9a10/Cookie.rar (sorry for that it's on dump, I can't get it to .zip :P)
Potemkyn
06-05-2008, 05:24 PM
I've always used frame labels and stored them in an array. Accessing the particular section by label would be like this:
function gotoSection(){
gotoAndPlay(sectionArray[lastLockVariableStatus]);
}
Remember, in an array, the first position is 0. So you'll either need to make the first entry in the array a blank ("") or subtract 1 from variable in the function [lastLockVariableStatus-1]
Since you are simply accessing a particular level, say level2, you could save the variable as a 2, then to access the particular level, try this:
function gotoLevel(){
gotoAndPlay("level"+lastLockVariableStatus);
}
I'm guessing that if you stored a 2, they can goto level2 and start playing there.
Were you able to try the shared object method? I'm reading up on it myself now.
Mike
trymedo
06-05-2008, 06:07 PM
shared objects are dead easy when you get used to it.
If you want I could post an example on how to use them tomorrow (Im just about to finish work)
It would be a lot easier to do it this way rather than talking to an external script to hold your data for you.
Just let me know what you'd like to do and I'll make an example in the morning.
ThePearl
06-05-2008, 06:16 PM
shared objects are dead easy when you get used to it.
If you want I could post an example on how to use them tomorrow (Im just about to finish work)
It would be a lot easier to do it this way rather than talking to an external script to hold your data for you.
Just let me know what you'd like to do and I'll make an example in the morning.
Yeah... I guess that it is, but right now I'm new to it, but I'm learning alot from this though...
If you could create any example of this it would be awesome! It's pretty simple actually. All I want it to do is to store the frame number when you hit a button or when you complete the level. Then you could load them with a "load" button.
But that will return in a dead end when you've completed the game... So is it possible to actually make sure that the user completed all the levels and then another button in the menu appear and you can click it and go to a level selector?
Let us just hope that this isn't too advanced... If not I might really learn alot from this!
Cheers
xxneon
06-05-2008, 09:13 PM
this little bit would go into your main timeline either before the menu or on the frame for the menu..
//setup an array that targets each levels target frame..
var targetFrames = [1,2,3,4,5,6];
var so:SharedObject = SharedObject.getLocal("levels");
if(so.data.levelsComplete == undefined){
so.data.levelsComplete = 0;
}
if(so.data.gameCompleted){
//make your special button appear in the menu..
}
in the code on the main timeline when you complete a level..
so.data.levelsComplete = 3; //whatever level they just beat..
so.flush();
in the code for your load button.. (main timeline again)..
load_btn.onRelease = function(){
var targetFrame:Number = targetFrames[so.data.levelsComplete];
gotoAndPlay(targetFrame);
}
and in the code when you beat the game..
so.data.gameCompleted = true;
so.flush();
the targetFrames array might be tricky to understand at first .. you just need to remember that level 1's target frame is accually at array index of 0..
so when you say that there is 0 levels completed .. it will take you to level 1.. if you have 1 level completed .. it takes you to level 2.. because ..
levelsComplete will equal 1.. and then when you hit the load button the target frame will be targetFrames[1] which accually points to level 2's target frame..
hope this makes sense... if not i might be able to break it down even more.. but these few things you want to study in the flash help..
SharedObject.getLocal() ~~ this loads the shared object into flash..
SharedObject.flush() ~~ this will save any changes made to the shared object to the file..
SharedObject.data ~~ this is the object where you store all your variables..
ThePearl
06-05-2008, 10:18 PM
Thanks alot! it was exactly this little snippet of code that I needed!
var targetFrames = [1,2,3,4,5,6];
var so:SharedObject = SharedObject.getLocal("levels");
if(so.data.levelsComplete == undefined){
so.data.levelsComplete = 0;
}
if(so.data.gameCompleted){
}
And also it was pretty simple when you see the code :rolleyes:.
And this should be "hack" proof, right? You can't skip any stage, because then the game isn't completed... Else I could just use a hittest between the player and the exit point and change the value whenever they collide.
Thanks everyone for the help! I will try the code out later, since I'm busy right now.
Trymedo, thank you for the offer, but I might not need it anymore,
and thank you xxneon!
Potemkyn
06-06-2008, 03:12 PM
xxneon, how long does the swfobject last?
Mike
ThePearl
06-07-2008, 12:41 AM
Until you clear out your cookies/saved files I guess? Else I have no idea since there isn't any stickaround or that kind of things in the script...
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.