SharedObject to save data to local drive AS3

SharedObjects are awesome, store variables locally on the users machine if you’re unable to use a database etc. to store it. Here’s some code from something using it to increase / store a viewCount variable.

public function createSo():void
{
//create the local Shared Object
try {
cookie = SharedObject.getLocal(String(moduleID)); // direct reference to the moduleID's data object in sharedObject.
} catch (e:Error) {
Utils.alert(this, "Cannot create local SharedObject. This tool will not be able to store any of your data. It is recommended you get support to enable a SharedObject for this location before continuing.");
}
if (!cookie) {
Error("Cookie on SharedObject.getLocal failed to return.");
} else {
// create the objectArray to hold each users account
if (cookie.data.userAccounts == undefined) {
cookie.data.userAccounts = []; // array to atore objects of user accounts.
}
}
}

/// Wipes the cookie from the users drive.
public function flushData():void
{
trace(cookie);
cookie.clear(); // wipes all data on the drive
cookie.flush(); // flushes the cookie file

createSo(); // creating a replacement cookie
}

public function increaseViewCount():void
{
trace("cookie.data.viewCount: " + cookie.data.viewCount);
if (cookie.data.viewCount == undefined) {
cookie.data.viewCount = 0;
} else {
cookie.data.viewCount++;
}

}
Tech Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑