02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

55 break;<br />

56 case 9:<br />

57 str = "Oc<strong>to</strong>ber ";<br />

58 break;<br />

59 case 10:<br />

60 str = "November ";<br />

61 break;<br />

62 case 11:<br />

63 str = "December ";<br />

64 break;<br />

65<br />

66<br />

}<br />

67<br />

68<br />

d = expdate.getDate();<br />

69 if (d<br />

return str;<br />

Listing 21.4<br />

Shared objects for persistent data<br />

A great feature of <strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> is the object ‘SharedObject’. This operates in a very similar<br />

way <strong>to</strong> cookies and can be used without any external scripting. To use the SharedObject you<br />

first create an object from the ‘getLocal’ method of the SharedObject. This takes a single string<br />

parameter, which is the name <strong>to</strong> use for the shared data file. Unless you want <strong>to</strong> read this file<br />

from another program then you do not need <strong>to</strong> know where the data is actually s<strong>to</strong>red; on a PC it<br />

is in the Documents and Settings folder: C:\Documents and Settings\[User Name]\Application<br />

Data\Macromedia\<strong>Flash</strong> Player\[path <strong>to</strong> swf]\swfname.swf. In the file the data for each s<strong>to</strong>red<br />

element is saved as a string. Having created a shared object from this file, the object contains the<br />

data in the file. If the file does not exist then the object is essentially empty. To use the object you<br />

can use the ‘data’ property and the ‘flush’ method. The data property contains any data that you<br />

want <strong>to</strong> s<strong>to</strong>re up <strong>to</strong> the file limit that is settable, but should be fairly small. In this short snippet,<br />

we create a shared object called ‘so’ then trace the value of the data property ‘favoriteColor’; this<br />

could be any name that you find useful. Then we assign <strong>to</strong> this property the value ‘orange’. If<br />

the property does not exist then it is created by an assignment. Then we access the data property<br />

‘possibleColors’. If this exists then the data will be listed in the output window and if it does not<br />

then ‘undefined’ will be displayed. If the array is missing then we create it as a new Array and<br />

assign values <strong>to</strong> it, then assign this array <strong>to</strong> the data property ‘possibleColors’. Finally we ensure<br />

that the data is written <strong>to</strong> the file using SharedObject method ‘flush’. If ‘flush’ returns false then<br />

writing <strong>to</strong> the file is prohibited by security limits or file size.<br />

336

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!