- Home
- Tutorials
- Flash
- Intermediate
- SharedObjects

Deleting SharedObjects and tidying up
Jesse Stratford
Jesse was born and raised in Australia, and now lives in London. He is one of the original founders of http://ActionScript.org, and was formerly a Flash developer, teacher, author and speaker. While Jesse no longer works as a full-time Flash professional, he still enjoys actively participating in the http://ActionScript.org community as time permits.
mySO = SharedObject.getLocal("test");
mySO.data.stickAround = "I'll be here for years to come!";
mySO.flush();
delete mySO;
// Reload the SO
mySO = SharedObject.getLocal("test");
delete mySO.data.stickAround;
mySO.flush();
// Delete the SO
delete mySO;
// Load the SO back in once more,
mySO = SharedObject.getLocal("test");
// Scan the SO for values
for (a in mySO.data) {
trace(a+": "+mySO.data[a]);
}
// Wohoo! No stickAround value!
Macromedia's docs don't show us how to remove the physical file from a user's drive, (although they say it's possible), but we can combine two things we know already to figure it out. Firstly, when we write a SO with no properties in its data child, no file is written. Secondly when we delete a property from the data child and re-write the SO the property is removed from the file on the disk also. So in order to delete the actual file, one need only delete all the properties of the data child. This is probably best done with a for...in loop.
// Make a basic SO
mySO = SharedObject.getLocal("test");
mySO.data.stickAround = "I'll be here for years to come!";
mySO.data.myArray = new Array(1, 2, 3, 4);
mySO.flush();
delete mySO;
// Reload the SO
mySO = SharedObject.getLocal("test");
for (a in mySO.data) {
delete mySO.data[a];
}
mySO.flush();
// Delete the SO
delete mySO;
// Load the SO back in once more,
mySO = SharedObject.getLocal("test");
// Scan the SO for values
for (a in mySO.data) {
trace(a+": "+mySO.data[a]);
}
// There are none. There's no file on the drive any more either
It's noteworthy that all the examples I've used in this tutorial cover creating and accessing the SO from the same SWF file. In practice this will rarely be the case, as we like to make our SWF files modular. To access a SO file created by a different SWF file you need to provide additional arguments to the getLocal() method. See this thread in which we show how it's done.
Finally, if you are determined to force your user to accept the SO you can advise them that SOs are required for your application and then run this code:
System.showSettings(1);
which will popup the local settings configuration box (see Figure 2.1) and allow the user to enable/disable SO storage and increase/decrease allocated SO storage space on a general scale.

Figure 2.1 Change Local Storage Settings Prompt
Viola! That's it! Thanks to Macromedia for their good docs on this. I'm off to bed. If this tutorial helped you out please drop me an email and let me know. I'm open to corrections and suggestions as always.
| Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |
Spread The Word
12 Responses to "SharedObjects" 
|
said this on 08 Mar 2007 2:40:55 AM CDT
Would've been nice if
|
|
said this on 02 Jun 2007 7:33:58 PM CDT
Can you add a function fo
|
|
said this on 18 Feb 2010 9:19:27 PM CDT
Go To http://forums.xgens
|
|
said this on 18 Feb 2010 9:23:22 PM CDT
// Use With 3 MovieClips
//Title va file1_mc gotoAndSt } file2_mc. s gotoAndSto } file3_mc.o sa gotoAndStop } //Add More if(saveFile = 1){ if(myLS //Output = out } else{ //O output_txt.te } myObj = {}; myObj.objArray[0] myObj.ob //Add More Arra myLSO.d } m if(myLSO //Output = outp } else{ / output_txt. variable1 = myL variable2 = myLSO.d } function deleteGame( myLSO = SharedObjec i //Output = } else //Output = outp myLSO.purge(); } } if(saveFile = function saveGame( myLSO = SharedObjec i //Output = } else{ output_ } myObj = myObj.objArray = myObj.objAr my myObj.objArray[2] //Add Mor m } function loadGame() myLSO = SharedObject if //Output = } else{ //Output = outpu variable1 variable2 = m variable3 = myLSO } } function delet myLSO = Share if(myLSO.data.myObj //Outp output_txt.text } //Output = myLSO.purg } } } if(save function sav myLSO = Share if(myLSO.data.myObj //Outp output_txt.text } e //Output = o } m myObj.objAr myObj myObj.objArray[1] = v myObj.objArr //A myLSO.data.myObj = m } function load myLSO = Shared if(myLSO.data.myObj = //Outpu output_txt.text = } //Output = var variabl variable3 = } } function myLSO = if(myLSO.data. output_txt } else{ //Outpu output_txt.text = myLS } } } / //Call U |
|
said this on 09 Jun 2007 4:16:21 AM CDT
thanks this code has solv
|
|
said this on 18 Jun 2007 1:21:42 AM CDT
this tut help me lot to u
but i am confused how h file |
|
said this on 06 Nov 2007 9:33:01 PM CDT
To load for a game would
previous_btn.onRelea previousEntry(); } In order to have thi Overall this in an int |
|
said this on 02 Jan 2008 11:56:24 AM CDT
u could load your data fr
holder = mySO.dat text1.text = hol then there goes.rel |
|
said this on 14 Jan 2009 1:51:44 PM CDT
this is so confusing with
|
|
said this on 22 Jun 2009 7:13:28 AM CDT
That's exactly what I'm l
|
|
said this on 04 Nov 2009 8:07:51 PM CDT
this is one of the easies
|




Author/Admin)