|
Using Shared Objects
/* A Shared Object is like a cookie that can be stored on your computer by the Flash Player.
Unlike normal browser JavaScript based cookies, most web browsers still do not come with
any built in ways to delete Shard Objects created by the Flash Player.
However, with an add on like BetterPrivacy for browsers like FireFox,
one can see, manage, and delete Flash Shared Objects.
A shared object is for the most part, harmless, websites use them to store information
such as last page viewed, links visited, last video watched (most common), and general user credentials.
In this case we'll use a shared object to remember your colour scheme from the last example.
Like the last example Javascript stored in an xml variable is used,
this time to get the current colour scheme being used.
*/
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.external.ExternalInterface;
import flash.net.SharedObject;
import flash.net.SharedObjectFlushStatus;
public class atadColourSettings extends Sprite
{
[Embed(source="vectors/grayButton.swf")]
private var grayButton:Class;
private var currentColors:String;
private var atadColor:SharedObject;
private var textReplacer:RegExp;
private var HTMLColours:XML;
public function atadColourSettings()
{
/* this time the function will return a variable */
HTMLColours =
<script>
<![CDATA[
function () {
var newColors = "cfatads0|207,170,128|D1D4FB|209,212,251";
try {
var bsix = document.getElementById('ubsix').value
var csix = document.getElementById('usix').value;
var brgb = document.getElementById('ubrgb').value;
var crgb = document.getElementById('urgb').value;
newColors = ""+csix+"|"+crgb+"|"+bsix+"|"+brgb+"";
} catch (e) {
newColors = "cfaa80|207,170,128|D1D4FB|209,212,251";
}
/* When using a return from inside xml like this, be sure that the id of the swf (in the html) is unique.
In IE8, using the developer tool, you can see how this function gets used in the DOM. */
return(newColors);
}
]]>
</script>;
/* The shared object is called atadColorChoices. */
/* The / means that all SWF files in the directory will be able to read the shared object,
so long as they too have a slash for the localPath parameter in the getLocal method */
atadColor = SharedObject.getLocal("atadColorChoices", "/");
textReplacer = new RegExp("Status[\:]{1}[a-zA-Z0-9\.\,\r\n ]{0,}[\:]{1}", "ig");
placeButtonsAndText();
}
private function doSave(e:MouseEvent):void
{
/* we do this so that the object is always re written */
if (atadColor.data.colorString) {delete atadColor.data.colorString;atadColor.clear();}
var defaultNewColorsAre:String = "cfaa80|207,170,128|D1D4FB|209,212,251";
currentColors = (ExternalInterface.available) ? ExternalInterface.call(HTMLColours) : defaultNewColorsAre;
atadColor.data.colorString = currentColors;
var success:String = "no";
var textFieldInQ:TextField = (getChildAt(0) as TextField);
/* if shared objects are disabled the catch would happen */
try {
success = atadColor.flush(15000);
var stext:String = "Status: Your colour choice has been saved. :";
var srtext:String = textFieldInQ.text.replace(textReplacer, stext);
textFieldInQ.text = srtext;
} catch (e:Error) {
var newstring:String = "Status: Shared Objects don't seem to be supported by your"+
"current configuration. \nClick settings, then advanced, "+
"in the right click menu of the Flash Player, "+
"to gain access to your global Flash Player settings. :";
var newtext:String = textFieldInQ.text.replace(textReplacer, newstring);
textFieldInQ.text = newtext;
success = "no";
}
/* success will equal this value if there is not enough space for the object,
and/or the user has to allow permission for shared object storage/space. */
if (success == SharedObjectFlushStatus.PENDING) {
atadColor.addEventListener(NetStatusEvent.NET_STATUS, userChoiceDetermine, false, 0, true);
}
}
private function userChoiceDetermine(e:NetStatusEvent):void
{
if (e.info.code == "SharedObject.Flush.Failed") {
var atext:TextField = (getChildAt(0) as TextField);
var stexte:String = "Status: You have chosen to disallow shared object storage. :";
var srtexte:String = atext.text.replace(textReplacer, stexte);
atext.text = srtexte;
}
}
private function doDelete(e:MouseEvent):void
{
if (atadColor) {
/* delete the shared object by calling clear */
atadColor.clear();
var thet:TextField = (getChildAt(0) as TextField);
var stextr:String = "Status: Your colour choice and shared object have been deleted. :";
var srtextr:String = thet.text.replace(textReplacer, stextr);
thet.text = srtextr;
}
}
private function placeButtonsAndText():void
{
var saveButton:Sprite = new grayButton() as Sprite;
var deleteButton:Sprite = new grayButton() as Sprite;
var saveText:TextField = new TextField();
saveText.text = "Save";
var deleteText:TextField = new TextField();
deleteText.text = "Delete";
var infoText:TextField = new TextField();
infoText.autoSize = TextFieldAutoSize.LEFT;
infoText.multiline = true;
infoText.wordWrap = true;
infoText.width = 250;
infoText.height = 300;
infoText.border = true;
infoText.mouseWheelEnabled = true;
infoText.text = "Use the colour selector in the previous example to change the colour scheme of the site,"+
" then come back here and press save to save your colour choice. "+
" \n Press delete to delete your saved choice."+
" \n After you have saved your colour choice,"+
" the site will remember the colour scheme you saved."+
"\n\nStatus::";
addChild(infoText);
saveButton.x = 20;
saveButton.y = infoText.height + 35;
saveButton.scaleX *= .5;
saveButton.scaleY *= .5;
saveButton.alpha = .5;
deleteButton.y = saveButton.y;
deleteButton.alpha = .5;
deleteButton.x = saveButton.x + 70;
deleteButton.scaleX *= .5;
deleteButton.scaleY *= .5;
saveText.selectable = false;
deleteText.selectable = false;
saveText.y = saveButton.y + 1;
deleteText.y = deleteButton.y + 1;
saveText.x = saveButton.x + 7;
deleteText.x = deleteButton.x + 7;
saveButton.addEventListener(MouseEvent.CLICK, doSave);
deleteButton.addEventListener(MouseEvent.CLICK, doDelete);
addChild(saveText);
addChild(deleteText);
addChild(saveButton);
addChild(deleteButton);
}
}
}
|