3.
Add more functions to your applications
Providing you are creating a game with Fash
and want to save and load the scores of players,
the features can be implemented in FFish Script.
You can save the scores in two ways, the first
one is to save the data in Windows Registry,
the second is to save the data in a file. At
here we choose to save the data in a File.
Consider the data structure for the scores.
We can use an array to save the data, for every
item in the array is an object, the object represents
the score information for a user. The object
can be constructed by the following function
in FFish Script.
function scoreInfo(playerName, score)
{
this.playerName = playerName;
this.score = score;
} |
In this sample we put the data file in the
same folder of the application.
SWFKit always inserts the "Initialize"
script into your project. When the application
built by SWFKit is launched, the "Initialize"
script will be called before the main window
appears on screen (the main movie hasn't been
loaded into the Flash Player yet).
You can do some initialization work in this
script. In this sample, you can load the scores
in the "Initialize" script.
// gets the data file name
scoreRecords = [];
var scoreFile = getAppDir() + "\\scores.txt";
// loads the data
DataFile.load(scoreFile);
// traces the data
for (i = 0; i < scoreRecords.length;
i++)
{
var score = scoreRecords[i];
trace(score.playerName);
trace(score.score);
} |
In the "Initialize" script, the
scores are loaded. Now we need to implement
the
"New Player" feature. Providing a
"New Player" button has been inserted
in
the Flash Movie, add the following action for
the button in Action Script.
| on (release)
{
fscommand("ffish_run", "newplayer");
}
|
When the player clicks the button, SWFKit will
call the "newplayer" script. In SWFKit,
insert a new script and rename it to "newplayer",
add the following code in it:
| scoreRecords.push(new scoreInfo(playerName,
score)); |
When the application is about to close, the
data should be saved. Put the following code
in the "Initialize" script.
When the application is about to close, the
"onClose" event of the main window
will fire. We can save the scores in the "onClose"
event handler.
| // Save the score info when close
var mainWindow = getMainWnd();
mainWindow.onClose = function ()
{
// remove the old file
var df = getAppDir() + "\\scores.txt";
DataFile.remove(df);
DataFile.save(df, "scoreRecords");
} |
|