^

Friday, January 27, 2012

Save and Load in Flash Game


Do you plan to create a huge game where it's impossible to beat it in one day? You need a Save and Load Feature in your game? You came to the right place because I'm going to show you how to do this. You can save and load information via cookies to your hard drive. The files generated are .sol files, and it's block text, there is no way someone can alter it to change his score or etc.



How to Save in Flash Game
Basically, when we save in flash game, it's the same with we save a value to some variable then store it to your hard drive. Ok, now lets make a project where we can save some information.

In the project, create a button and change it's instance name to btnSave (or whatever you want). Then create two text box (input text) and change its instance name to txtName and txtPoint.
Place this code:


_global.thename=" ";
_global.point=0;
_global.savefile = SharedObject.getLocal("YourGame");

btnSave.onRelease = function () {
 _global.thename = txtName.text;
 _global.point = txtPoint.text;

 _global.savefile.data.thename = _global.thename;
 _global.savefile.data.point = _global.point;
}

First we define a global variable for name and point, then we create a cookies YourGame for saving and loading game. When you click the save button, your game will take name and point value from variable _global.name and _global.point and store it to YourGame cookies.

How to Load in Flash Game
Now, create another button and change its instance name to btnLoad (or another name you like).
Add this code:
btnLoad.onRelease = function() {
 _global.thename = _global.savefile.data.thename;
 _global.point = _global.savefile.data.point;
}

In the code above, we take point and name value from savefile and store it to our global variable name and point.

Test your Save and Load!
Create another button and change its instance name to btnRefresh.
Add this code:
btnRefresh.onRelease = function() {
 txtName.text = _global.thename;
 txtPoint.text = _global.point;
}


Now, lets test your game. Open it, input name and point in the text box. Click save button. Then Close your game. Now open your game again, then click load button. Click refresh button.

I know that you can do a simple way to do it, you can take direct value from txtName and txtPoint without global variable. I just want to show you that we can load a savefile to some variables. Have fun!

Oh, this is the source code: SaveAndLoad (CS4)

Keyword (Indonesia) : save dan load di game flash
Image : iconspedia.com

3 comments: