Flash Tutorial for Web Developers: Save/Load Info Using Cookies

By Community Member FlashKit

Have you ever made a huge Flash game and knew that there was no way a person could beat it in one day? Well, now you can save and load information via cookies so the game can resume when they're ready with this tutorial from the FlashKit community!

To start with, the files that are generated are .sol files, and that data is saved as block text so there is no way someone can alter it to changen their score, etc. I'm going to show you how to use cookies to do this using ActionScript.

Start up Flash and create a button--for our example this will be the "Save" button. Put this in its ActionScript panel like so:

on (release)
{
_root.pos = sharedobject.getLocal("COOKIE NAME");
_root.pos.data.guy = _root.guy._x;
_root.pos.data.guy = _root.guy._y;
_root.pos.data.NAME = _root.NAME;
}
Ok, let's take this step by step...
_root.pos = sharedobject.getLocal("COOKIE NAME");
This means that you have just created "pos", which is the cookie file itself. For "COOKIE NAME", name it whatever you want, but make sure that it is appropriate for the type of save for easy remembering, i.e.: "player 1 save" or something like that.
_root.pos.data.guyx = _root.guy._x;
_root.pos.data.guyy = _root.guy._y;
_root.pos.data.NAME = _root.NAME;
This is basically what it's saving. It records the MC "guy"'s _x and _y positions into pos' data. Of course, you don't have to name them guyx and guyy, you can name them anything, but don't change _root.guy._x and _y, because those are the actual targets. NAME is the instance name for items such as a dynamic text box for saving the score, or anything like that.

Now for the load button--create one and put this in the ActionScript panel:

on (release) {
_root.pos = sharedobject.getLocal("COOKIE NAME");
setProperty(_root.guy, _x, _root.pos.data.guyx);
setProperty(_root.guy, _y, _root.pos.data.guyy);
_root.NAME = _root.pos.data.NAME;
}
This sets the properties for the things that are saved from the stage. For those that aren't, you must use the attachMovieClip() function and then set its properties.

That's it! Now you can create games which hold the scores, user information, position in the game, etc. Now start making some games!

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date