Example: Interacting with the user
We will create an application that asks the user his or her name, saves
it and uses it when the user starts the application next time. To save
this user-defined data, we will use the advanced FSCommand features.
Do the following to create an application that asks and later uses the
user's name:
- Open SWF Maestro EXE.
- Click “New>New From...” and select one of the project
files created in the previous examples.
- Open “Files > Files for compilation ”.
- Select “Compile One SWF file” and specify the path to
the file “interaction.swf” in the “Tutorials\Interaction” directory.
It uses ‘FSCommand’ calls to work with the data storage
in order to save/use the user-defined name.
- Click “Save As” to save the project.
- Click Preview to test the application.
You will find the application FLA file out of which “interaction.swf” was
created in the 'Tutorials\FLAs' directory.
interaction.swf description:
Consists of 3 frames.
The 1st frame contains the following ActionScript:
stop();
var UserName="";
function watchCallback (id, oldval, newval) {
if(newval=="") {
gotoAndStop(2);
} else {
gotoAndStop(3);
}
};
_root.watch("UserName", watchCallback);
fscommand("storage.get","UName,UserName");
Comments:
stop();
We stop the playback to initialize the application and
check whether we know the user's name.
var UserName="";
We initialize the variable into which we
will read the user's name.
function watchCallback (id, oldval, newval)
This function
will be used right after the record from the data storage will be read.
It analyzes the value of the records and jumps to the 2nd frame if
the name is unknown, otherwise it jumps to the 3rd frame.
_root.watch("UserName", watchCallback);
We use this watch function call to enable listening to the UserName
variable. When the variable gets the value, the watchCallback function
will be called.
fscommand("storage.get","UName,UserName");
We use this call to read the value of UName from the data storage and
assign it to the UserName variable.
Note. We cannot just read the value using FSCommand(“storage.get”,””)
and jump to the necessary frame because the FSCommand function is asynchronous
and returns execution immediately before the value is actually assigned.
That is why we need to introduce the listening function that is executed
after the value is assigned.
The 2nd frame contains an “Input Text” field named “InputName” and “PushButton” with “Click
Handler = onConfirm”.
The 2nd frame contains the following ActionScript:
function onConfirm(component){
FSCommand("storage.set","UName,"+InputName.text);
gotoAndStop(1);
}
Comments:
This function is executed when you click the button and saves the user-defined
text (username) in the data storage as a value named “UName”.
The 3rd frame contains a “Dynamic Text” field named “DynaText” and “PushButton” with “Click
Handler = onReset”.
The 3rd frame also contains the following ActionScript:
function onReset(component){
FSCommand("storage.clear");
gotoAndStop(1);
}
function watchCallback (id, oldval, newval) {
if(newval=="") {
gotoAndStop(2);
} else {
DynaText.text="Hello, "+newval;
}
};
_root.watch("UserName", watchCallback);
fscommand("storage.get","UName,UserName");
Comments:
function onReset(component) { }
This function is executed when the button is clicked and clears the data
storage. The rest of the code reads the value of UName from
the data storage and, if the value is empty, jumps to the 2nd frame,
otherwise it puts the welcome string into the text field. |