Tutorials>Advanced>Save Data
This tutorial will teach you how to save data, variables. If you are going to do this guide, or use saving in the future make sure that 100kb of information can be stored on your computer (To check open a swf, right-click on the screen > settings > Folder Tab > Move slider to 100kb. Also make sure Never is not checked).
Now instead of using cookies to save information we will be using SharedObject();
Storing Information
Before creating things to be saved you need to define where the data will be saved to.
On the first frame add
We created local as the new object and have the data saved at "test". SharedObject allows for data to be saved locally.Code:local = SharedObject.getLocal("test");
We now check to see if data was previsouly saved for user name. If the users name is missing then we need to stop at the current frame [frame 1].
SETTING UP THE TEXTCode:if (local.data.user == undefined) { gotoAndStop(1); }
We now need to setup a new text format.
We then create two text fields with 'as' [action script]. The first text field tells the user to create a user name and the second text field is an input box.Code:var myStyle = new TextFormat(); with (myStyle) { font = "Arial"; size = 10; color = 0x000000; align = "left"; }
THE BUTTONCode:dDepth = 1; this.createTextField("telluser", dDepth, 75, 15, 120, 15); with (telluser) { selectable = false; setNewTextFormat(myStyle); text = "Please create a User Name:" } dDepth++; this.createTextField("userinput", dDepth, 75, 30, 120, 15); with (userinput) { background = true; type = "input"; setNewTextFormat(myStyle); } dDepth -= 1;
We now need to create a button. So on the play field create a circle with the dimensions 15 x 15. Convert Symbol to a button called "create username". Place the button at x: 150 and y: 30. We now need to tell the button to save the data that the user input into the "userinupt" text box we made.
Now to make saveName actully save the data we need to return to the first frames actions, saveName needs to become a function:Code:on (release) { saveName(userinput.text); }
All the data has been saved onto the users hard drive as "test". If you wanted you could add to the button that the playhead moves to a certain frame to tell the user data has been saved [if you do this make sure that it sends playhead to frame 3],.Code:function saveName(name){ local.data.user = name; }
RECALLING
We now need to make it so if the user exits the script then returns, his data gets recalled. We need to return to the first frames actions.
This says, if there is information saved that it goes to another frame and bypasses all the previous coding that we did so the user does not have to create a new name. Now to recall the information. Open up the second frames actions and add:Code:if (local.data.user == undefined) { gotoAndStop(1); } else { gotoAndStop(2); }
This states that the saved data is placed in "test".Code:local = SharedObject.getLocal("test");
MORE TEXT
THE FINAL PRODUCTCode:dDepth = 1; this.createTextField("telluser2", dDepth, 75, 15, 120, 15); with (telluser2) { selectable = false; setNewTextFormat(myStyle); text = "Your user name is:" } dDepth++; this.createTextField("userdisplay", dDepth, 75, 30, 120, 15); with (userdisplay) { background = false; selectable = false; type = "dynamic"; setNewTextFormat(myStyle); text = local.data.user } dDepth -= 1;
Actions for Frame 1:
Actions for button "create username" in frame 1:Code:local = SharedObject.getLocal("test"); if (local.data.user == undefined) { gotoAndStop(1); } else { gotoAndStop(2); } var myStyle = new TextFormat(); with (myStyle) { font = "Arial"; size = 10; color = 0x000000; align = "left"; } dDepth = 1; this.createTextField("telluser", dDepth, 75, 15, 120, 15); with (telluser) { selectable = false; setNewTextFormat(myStyle); text = "Please create a User Name:"; } dDepth++; this.createTextField("userinput", dDepth, 75, 30, 120, 15); with (userinput) { background = true; type = "input"; setNewTextFormat(myStyle); } dDepth -= 1; function saveName(name) { local.data.user = name }
Actions for Frame 2:Code:on (release) { saveName(userinput.text); }
DIFFERENT WAYS OF SAVINGCode:local = SharedObject.getLocal("test"); dDepth = 1; this.createTextField("telluser2", dDepth, 75, 15, 120, 15); with (telluser2) { selectable = false; setNewTextFormat(myStyle); text = "Your user name is:" } dDepth++; this.createTextField("userdisplay", dDepth, 75, 30, 120, 15); with (userdisplay) { background = false; selectable = false; type = "dynamic"; setNewTextFormat(myStyle); text = local.data.user } dDepth -= 1;
With SharedObject you can create all sorts of different data to be saved. If you want to be able to just define variables without having a text box use:
local.data.name = 1;
**local can be changed to whatever object you have for SharedObject.
**name will also be changed depending on what you had the data saved to.
**The number 1 will be changed to whatever you want the data to be saved at, being a number, text, a symbol or a combo of all 3.
I find this way of saving data very useful and easy. Once you get use to it you can have tons of different data being saved at once. If you have any questions feel free to ask.
cheers,
crowebird
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks