Monday, April 2, 2012

AS2: Save Data

Source file available here: http://adf.ly/6xzBr


Welcome! In this tutorial, I will show you how to save data such as variables, text box data, char positions, and much more! This is handy if you need a "Save Game" option.


Alright, to get started, open up a new AS2 document.


First, make a button symbol. Call it "saveBtn". No instance name required.
Then, make another button. Call it "loadBtn". Still, no instance name.


Now for this tutorial, I will show you how to save a player's position on the screen.
So go ahead an create a character and name it "charMc". Give it an instance name of charMc.






















Now, let's make the character move with the arrow keys. Right click on our charMc movie clip and select "Actions". In the actions, type:



onClipEvent(load){
speed=3;
}
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x+=speed;
}
if(Key.isDown(Key.LEFT)){
this._x-=speed;
}
if(Key.isDown(Key.DOWN)){
this._y+=speed;
}
if(Key.isDown(Key.UP)){
this._y-=speed;
}
}


That is all the coding we need for charMc. All we have to do now, is enter in save and load actions in the buttons. In the "saveBtn" button, type:



on(release){
_root.pos = SharedObject.getLocal("tutorial");
_root.pos.data.charMc_x = _root.charMc._x;
_root.pos.data.charMc_y = _root.charMc._y;
}


And in the "loadBtn" button, type:



on(release){
_root.pos = SharedObject.getLocal("tutorial");
_root.charMc._x = _root.pos.data.charMc_x;
_root.charMc._y = _root.pos.data.charMc_y;
}


And that's it!
On the first line of the save and load code, we are making a new cookie that will be stored locally on the player's computer. Next line on the save code, we call the "charMc_x" and "charMc_y" variables to store the player's position. In the load button, we search for that variable in the cookie and display it. If you wanted to save the score, you could use this in the save button:
_root.pos.data.score = _root.score;
And then in load button, just flip it around!
Hope this tutorial was helpful!


Source file available here: http://adf.ly/6xzBr

Sunday, April 1, 2012

I'm Back!

Sorry for the lack of tutorials!
I was designing a game for a state competition. Now that it's over, it's time for some amazing tutorials! Be ready for constant weekly updates :]

-Eric