next up previous contents
Next: Using FFish script objects Up: Tutorials Previous: Converting your flash movies   Contents


Add more functions to your application

Besides simply converting a flash project to an .exe file, you may want to add more functions to your application such as reading and writing disk files, accessing databases, encrypting and decrypting files, etc, which Flash itself does not provide. In this case, you would have to use the FFish Scripting language.

The FFish Scripting language is a javascript style scripting language, that is, if you know the javascript programming or the actionscript programming, you also know the ffishscript programming. The FFish scripting language provides many objects to extend the power of Flash. More details of these objects can be found in the FFish scripting language manual.

The general way of using the FFish scripting language is to insert scripts into SWFKit, add code into the scripts and call the script in Action scripts. If you use Flash 8, however, you can call the FFish script objects directly from within Action script. This way is more clear and convenient. Calling the FFish script objects directly in Action script will be introduced in another tutorial, at here we only discuss how to use the FFish scripting language in the former way.

First, we would like to introduce the "initialize" script. Maybe you have found that for every newly created SWFKit project, there is a predefined "initialize" script. SWFKit automatically inserts an "initialize" script for each SWFKit project, and the "initialize" script will be called after the produced .exe file or .scr file is launched. It is called just after the main window is created and about to be visible, but before the main flash movie is loaded. Hence, you can use the "initialize" script to do some initialization tasks, for example, loading a data file, checking some registry entry, etc. A useful example is to make a program has only one instance at a time. The idea of doing this job is to find whether there is already a window has the same caption as the current program that is about to run. If such a window is found, we will bring the found window to top and stop the current program; otherwise, we will continue the current program. The following code shows how to do this:

var wnds = Window.getWindowsByName(your window name);
var w = getMainWnd();
var has = false;
for (i = 0; i < wnds.length; i++)
{
    if (wnds[i].handle != w.handle)
    {
        has = true;
        break;
    }
}

if (has)
{
    wnds[i].bringToTop();
    return false;
}

return true;
What you should notice is that when calling the "initialize" script the main movie is not loaded yet. All Attempts in the "initialize" script to set the values of the variables in the main movie will have no effect.

Second, you can add as many scripts in SWFKit as you can and call them in Action script. At here we only discuss the asynchronous way of calling FFish scripts - using the "ffish_run" fscommand. For example, say you have a script named "recordSound" in SWFKit, and you want to call it when you press a button, you can do as follows in Action script:

on (click)
{
    fscommand("ffish_run", "recordSound");
}
However, the fscommands does not run immediately after you click the button. It will not be called until the Flash Player has time to process user messages, that is, Action script code and fscommand are not called synchronously. For example, your call a script to change the value of a variable in the main movie, however, you cannot think that the value of the variable is changed immediately after the script is called by fscommand, because the value of the variable may be changed after all the Action script instructions in this frame has been executed. Notice: fscommands are always called synchronously, for example, if you have more than one fscommands, such as
...
fscommand("ffish_run", "script1");
...
...
fscommand("ffish_run", "script2");
"script2" will always be called after "script1", that is, the order of calling fscommands will never be reversed. Now we use an example to show how to use the FFish scripting language to add more functions to your SWFKit project.

Providing you are creating a game with flash, which will save and load the scores of players, that is, the game needs read and write a disk file to load and save data. To implement this feature, we would have to maintain an array to save the score data for each user, and add functions to add, load and save score data. The score information of a player is represented by the following object:

function scoreInfo(playerName, score)
{
    this.playerName = playerName;
    this.score = score;
}

First, when the game is launched, we should load the score data from a data file. We will do it in the "initialize" script.

function scoreInfo(playerName, score)
{
    this.playerName = playerName;
    this.score = score;
}

// gets the data file name
// The scoreRecords array is used to maintain all score data
scoreRecords = [];
var scoreFile = getAppDir() + "\\scores.txt";
// loads the data
DataFile.load(scoreFile);

// traces the data to see how many score data is loaded
for (i = 0; i < scoreRecords.length; i++)
{
    var score = scoreRecords[i];
    trace(score.playerName);
    trace(score.score);
}

Second, we would have to add a new script to add a score record for a new player. Generally there should be a button in the main movie; when pressing the button, we call the FFish script to add the a score record. After creating a new script called "newplayer" in SWFKit, we add the following code into the script:

// Get player name and score from flash movie
var playerName = FlashPlayer.getVariable("_root.playerName");
var score = parseInt(FlashPlayer.getVariable("_root.score"));
// Create a new score record and save into the array
var scoreRecord = new scoreInfo(playerName, score);
scoreRecords.push(scoreRecord);

Calling the "newplayer" script in Action script:

on (release)
{
    FSCommand("FFish_Run", "newplayer");
}

Finally, we would have to add a function to save the score data when the game exits. We may also add a button in the movie to save the score data, however, it is better to make the game save the score data automatically when it is about to exit. This can be done by handling the "onClose" event of the main window of the game. When the main window of the game is about to close, this event handler will be called. Hence, we can call the saving score data function in this event handler. We put this event handler into the "initialize" script because the "initialize" script has already been loaded into memory when the game starts. If you put the event handler in another script, you must make sure that the script is loaded, or the game cannot find the event handler.

function saveScores()
{
    var df = getAppDir() + "\\scores.txt";
    // remove the old file
    DataFile.remove(df);
    DataFile.save(df, "scoreRecords");
}

var wnd = getMainWnd();
wnd.onClose = function()
{
    saveScores();
}


next up previous contents
Next: Using FFish script objects Up: Tutorials Previous: Converting your flash movies   Contents
Copyright ©2000-2007 Shanghai TopCMM Software Technologies. All Rights Reserved.