If you are trying to understand how the SharedObject works with Flash CS3, follow this tutorial and you will soon know a lot more about this topic.
The ShareObject class is used to read or "store" data on the user"s computer.* To understand better, an easy comparison for the SharedObject would be the browser"s cookies.
This class is very useful and used a lot in game"s development.
I will make an example.
Let us say that we developed a game with Flash CS3.* The user will need to insert its own details so to be able to store his score and his name.* If the user would want to play more games, he would often be annoyed by the fact of having to enter his details once again at each game.
Instead, using the SharedObject class, our user could play as many games as he wants, entering once his personal details which would be memorized in a file by Flash and kept in the user"s computer.
The following example will ask you to insert your name, city and age.* Once "SALVA" clicked, Flash will save those data on your computer.* If you do a refresh at that point, you will see that Flash has retrieved your details successfully.
You can also delete your details and this time, after a page refresh, you will be asked to enter your details once again.
I create a FLA and save it as "main.fla".
Into which:
Frame 1:
- a MovieClip with an instance name "form_mc" which will contain 3 dynamic text fields "nome_txt", "citta_txt" and "eta_txt". Those three text fields need to be then declared as input so that the user can overwrite them. It will also contain a button with an instance name "save_btn".
- a MovieClip with an instance name "info_mc" which will contain a dynamic text field named "info_txt" and a button named "delete_btn".
- a dynamic text field named "output_txt"*.
Frame 2:
- a static text field which will advise that the data have been saved correctly.
.
*I create a Document Class, as AS file saved as "Main.as", implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.SharedObject;
import flash.net.SharedObjectFlushStatus;
public class Main extends MovieClip
{
private var _so:SharedObject;
public function Main()
{
stop();
init();
checkSharedObject();
}
private function init():void
{
form_mc.visible=false;
form_mc.x=stage.stageWidth/2;
form_mc.y=stage.stageHeight/2;
info_mc.visible=false;
info_mc.x=form_mc.x;
info_mc.y=form_mc.y;
form_mc.save_btn.label='SALVA';
info_mc.delete_btn.label='CANCELLA I TUOI DATI';
}
private function checkSharedObject():void
{
_so=SharedObject.getLocal('FlepStudio');
if(_so.data.nome==undefined||_so.data.citta==undefined||_so.data.eta==undefined)
displayAll();
else
displayInfo();
}
private function displayAll():void
{
form_mc.visible=true;
form_mc.save_btn.addEventListener(MouseEvent.CLICK, saveCookie);
}
private function displayInfo():void
{
info_mc.visible=true;
info_mc.delete_btn.addEventListener(MouseEvent.CLICK, clearCookie);
info_mc.info_txt.text='Tu sei '+_so.data.nome+'\n'+
'abiti a '+_so.data.citta+'\n'+
'ed hai '+_so.data.eta+' anni';
}
private function saveCookie(evt:MouseEvent):void
{
var flushStatus:String=null;
_so.data.nome=evt.currentTarget.parent.nome_txt.text;
_so.data.citta=evt.currentTarget.parent.citta_txt.text;
_so.data.eta=evt.currentTarget.parent.eta_txt.text;
try
{
flushStatus=_so.flush(10000);
}
catch (error:Error)
{
trace('Errore...non riesco a scrivere i dati su disco');
}
if(flushStatus != null)
{
switch(flushStatus)
{
case SharedObjectFlushStatus.PENDING:
output_txt.appendText('Richiesta permessi per salvataggio oggetto in corso...\n');
_so.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
break;
case SharedObjectFlushStatus.FLUSHED:
gotoAndStop(2);
break;
}
}
}
private function onFlushStatus(event:NetStatusEvent):void
{
output_txt.appendText("L'utente ha chiuso la finestra di dialogo.\n");
switch (event.info.code)
{
case "SharedObject.Flush.Success":
output_txt.appendText("L' utente ha confermato il permesso.\n");
break;
case "SharedObject.Flush.Failed":
output_txt.appendText("L' utente ha negato il permesso.\n");
break;
}
output_txt.appendText("\n");
_so.removeEventListener(NetStatusEvent.NET_STATUS,onFlushStatus);
}
private function clearCookie(mevt:MouseEvent):void
{
info_mc.info_txt.text='I tuoi dati sono stati cancellati dallo Shared Object, chiudi e riapri il browser e vedrai...';
delete _so.data.nome;
delete _so.data.citta;
delete _so.data.eta;
}
}
}
Let us analyse the code
Properties
An instance of the SharedObject class
private var _so:SharedObject;
Constructor function
I stop the time line at the first frame
stop();
I call the method "init"
init();
I call the method "checkSharedObject"
checkSharedObject();
Methods
init();
I impost "form_mc" to invisible and position it to the stage centre
form_mc.visible=false;
form_mc.x=stage.stageWidth/2;
form_mc.y=stage.stageHeight/2;
I impost "info_mc" to invisible and position it to the stage centre
info_mc.visible=false;
info_mc.x=form_mc.x;
info_mc.y=form_mc.y;
I assign a text to the button"s labels
form_mc.save_btn.label='SAVE';
info_mc.delete_btn.label='DELETE YOUR DATA';
checkSharedObject();
I check if an object named "FlepStudio" written by Flash already exists on the user"s computer, using the method "getLocal"
_so=SharedObject.getLocal(FlepStudio);
if it does exist, I check that the 3 properties "nome", "citte" and "eta" are present. If one or all of them are undefined, I call the method "displayAll"
if(_so.data.nome==undefined||_so.data.citta==undef ined||_so.data.eta==undefined)
displayAll();
otherwise if the three properties are defined, I call the method "displayInfo"
else
displayInfo();
displayAll();
"form_mc" is visibile as no object has been found
form_mc.visible=true;
I add a listener to the CLICK of "save_btn" placed into "form_mc". The event will call the method "saveCookie"
form_mc.save_btn.addEventListener(MouseEvent.CLICK , saveCookie);
displayInfo();
"info_mc" is visibile as an object has been found
info_mc.visible=true;
I add a listener to the CLICK of "delete_btn" placed into "form_mc". The event will call the method "clearCookie"
info_mc.delete_btn.addEventListener(MouseEvent.CLI CK, clearCookie);
I display the information retrieved from the object found on the user"s computer
info_mc.info_txt.text='You are'+_so.data.nome+'\n'+'you live in'+_so.data.citta+'\n'+'and you are'+_so.data.eta+' years old';
saveCookie();
a create a String variable and I impost its value to null to check if the operation will success
var flushStatus:String=null;
I assign three properties to the data object "_so" (instance of SharedObject) which at their turns will assign the written text to the text fields
_so.data.nome=evt.currentTarget.parent.nome_txt.te xt;
_so.data.citta=evt.currentTarget.parent.citta_txt. text;
_so.data.eta=evt.currentTarget.parent.eta_txt.text ;
I should now check once again that all the fields have been completed.
I let you deal with that part and start the process of writing
try
{
I assign to flushStatus a value returned by the method "flush" of the SharedObject class
flushStatus=_so.flush(10000);
}
catch (error:Error)
{
If the process is not successful, I trace an error message. This message could also be displayed to the user if wanted.
trace("Error...can't write data on disk");
}
if(flushStatus != null)
{
If the process of writing has been successful, I check the value of "flushStatus"
switch(flushStatus)
{
If the value of "flushStatus" is pending (returned by the event PENDING of the SharedObjectFlushStatus class), it certainly means that the user has a security system which requires confirm before accepting the cookie. So the user should be advised that he needs to accept the cookie to proceed. At this point, I must check if the user accept it or not. To do it, I add a listener to "_so" for the event NET_STATUS of the NetStatusEvent which will then call the method "onFlashStatus"
case SharedObjectFlushStatus.PENDING:
output_txt.appendText('Requesting permissions...\n');
_so.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
break;
If the value of "flushStatus" is "flushed", I move the play head to frame 2 of the main time line which will tell the user that the process has been successful
case SharedObjectFlushStatus.FLUSHED:
gotoAndStop(2);
break;
}
}
onFlashStatus()
at this time, the user will have accept or not the authorisation
output_txt.appendText("User closed the dialog.\n");
next, I will check, based on the value of the property "code" of the object "info" (itself from the event PENDING from the SharedObjectFlushStatus class). If accepted, I advise the user via the "output_txt"
switch (event.info.code)
{
case "SharedObject.Flush.Success":
output_txt.appendText("User confirmed permission.\n");
break;
if it has not been confirmed, I advise anyway the user that the authorization has not been given
case "SharedObject.Flush.Failed":
output_txt.appendText("User denied permissions.\n");
break;
}
I remove the listener NetStatusEvent.NET_STATUS
_so.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
clearCookie();
this method is called if the user decide to delete his details, Once again I advise him with a message
info_mc.info_txt.text='Data has been deleted form Shared Object, refresh your browser and you'll see...';
I remove the three properties created first to the Sharedobject "_so"
delete _so.data.nome;
delete _so.data.citta;
delete _so.data.eta;
Source files: