I've put together some sample code and files to help you gain a better understanding of Flash Lite 1.1 development. The sample code and files presented here are meant to give you an idea of how to solve some common problems that you may run into while developing content.
You might be wondering at this point how you can use Flash 4 ActionScript in your Flash Lite 1.1 content. I've selected a few of the most commonly used ActionScript commands and provided some sample code for each instance. These examples give you an idea of how to use a particular command. You should be able to apply the basis of these code examples to your own work.
getProperty()When you use getProperty(), you can retrieve a value for any of the built-in properties of a movie clip. This is often used in conjunction with the setProperty() function.
Example:
cartoonArea_x = getProperty(cartoonArea, _x);
setProperty()This allows you to specify one of several properties for a movie clip instance, which is very useful for repositioning objects on the Stage.
Example:
setProperty(cartoonArea, _x, 100);
tellTarget()Another one of the forgotten ActionScript commands, tellTarget() was
the original way of communicating between two movie clips, directing a movie
clip either to play, stop, or go to a particular frame. You're going to have
to use this often for Flash Lite 1.1 development, so get used to it!
Example:
tellTarget("_level0/headlines/title") {
gotoAndStop(1);
}getURL()Flash Lite 1.1 supports the getURL()action, which is processed
once per frame or per event handler and can only be associated with the following
keys: 0–9, *, #, or the Select key. If you have more than one getURL() call
in the same statement block, only the first call is executed.
Example: Loading a web page into the mobile phone's browser
getURL("http://mobile.flashdevices.net/");It's worth mentioning that certain handsets require a keyEvent to process getURL() calls. Basically this means the user needs to press a key on the handset to initiate the getURL() call; it can't occur as a frame event.
In addition, these calls will only be processed once per frame or per event handler. Keep this in mind as you develop your content.
Refer to the CDK for the platform you're developing for to get additional information.
eval()This function lets you dynamically generate variables within your application.
Example: Defining two variables and then creating a new variable based on those two variables
num = 2;
team2 = "Red Sox";
baseballTeam = eval("team" add num));
// result is "Red Sox"loadVariables(), loadVariablesNum()As with the desktop version of Macromedia Flash Player, you can use loadVariables() and loadVariablesNum() to load in data from a web server using the data connection on a mobile phone. By using these functions, you can update Flash content that resides on a mobile phone and make it dynamic.
Example 1: Loading variables into a specific movie clip using the loadVariables()function
loadVariables("http://www.flashdevices.net/data/scores.txt", "scores_mc");Example 2: Loading variables into a specific level using the loadVariablesNum()function
loadVariablesNum("http://www.flashdevices.net/data/news.txt", 5);Again, the loadMovie() calls have the same exceptions and rules as I mentioned above in the getURL() section.
loadMovie(), loadMovieNum()You can load external SWF files from a web server into a SWF file that already resides on a user's handset which these functions. One possible use would be to provide a new image to the user that would be displayed in the original application.
Example 1: Loading a new SWF file into a specific movie clip using the loadMovie()function
loadMovie("http://www.flashdevices.net/data/dailyPhoto.swf", "mainScreen_mc");Example 2: Loading a new SWF file into a specific level using the loadMovieNum()function
loadMovieNum("http://www.flashdevices.net/data/dailyPhoto.swf", 10);Again, the loadMovie() calls have the same exceptions and rules as I mentioned above in the getURL() section.
on(keyPress)One of the most basic ActionScript commands, this is used to detect when a user presses a key on the handset.
Example:
on (keyPress "<PageDown>") {
FSCommand2 ("Quit");
}Two commands, Escape and Unescape, are available to encode and unencode strings to and from formats that are safe for network transfer as they pass between servers and mobile phones.
EscapeThis function encodes an arbitrary string into a format that is safe for network transfer. It replaces all characters that are not alphanumeric with a hexadecimal escape sequence (%xx or %xx%xx in the case of double-byte characters). It returns the encoded string in a variable that is passed into the SWF file by name. This function is executed immediately upon invocation.
Here is the syntax:
status = FSCommand2( "Escape", original, encoded )
where original is the string to be encoded into a format safe
for URLs and encoded is the resulting encoded string.
Example:
original_string = "hello, how are you?";
status = fscommand2("Escape", original_string, "encoded_string");UnescapeThis function decodes an arbitrary encoded string that is safe for network transfer into its normal form. All characters that are in hexadecimal format—that is, a percent character (%) followed by two hexadecimal digits—are converted into their decoded form. It returns the decoded string in a variable that is passed in by name. This function is executed immediately upon invocation.
Here is the syntax:
status = FSCommand2( "Unescape", original, encoded )
where original is the string to be decoded from a format safe for network transfer and encoded is the resulting decoded string.
Example:
original_string2 = "Hello%7B%5BWorld%5D%7D";
status = fscommand2("Unescape", original_string2, "normal_string");scrollYou can use the scroll property to retrieve and set the contents of a text box. When you retrieve the scroll property of a text box, it indicates the number of the line currently displayed as the first line in the text box's viewable area. When you set the scroll property to a specific value, it scrolls the text field, making the specified line number the top line in the field's viewable region.
Example:
on (release) {
mainText.scroll = mainText.scroll + 1;
}
maxscrollThe maxscroll property returns the largest allowable scroll value for a text field. It represents the number of the last line in a text field that can be used as the top line in its viewable region.
Example:
textBoxMax = mainText.maxscroll
FSCommand2()The FSCommand2() function is a new ActionScript function that
is supported in Flash Lite 1.1 but not yet supported in the standard desktop
version of Flash Player. It provides similar functionality to the FSCommand() function, but its main benefits are that it:
An example of an FSCommand2() function would be the GetBatteryLevel() command, which retrieves the current battery level of the handset as a numeric value.
Example:
batteryLevel = FSCommand2( "GetBatteryLevel" )
Here are some examples of how to control movie clips in Flash Lite 1.1 using tellTarget:
// telling a movie clip to go to and play a label
tellTarget("../photoArea"){
gotoAndPlay("begin");
}
// telling a movie clip to go to and stop at a frame
tellTarget("/nameList"){
gotoAndStop(1);
}
// telling a movie clip to load a SWF file from a web server
tellTarget("../photoArea"){
loadMovie("http://www.flashdevices.net/data/pen.swf", "/icon_mc");
}If you have audio playing in your Flash application, and you want to stop all sounds that are playing, just use the following ActionScript code inside a button or on a frame event:
// stopping all sounds stopAllSounds();
If you want your application to open a web page on the user's handset, you can do this by using the following ActionScript code:
// opening up a web page
on(keyPress"<Enter>"){
getURL("http://mobile.flashdevices.net/");
}Note: This uses the default browser on the user's handset.
Although it is not possible to play back video within Flash Lite content, you can simulate this effect by using some ingenuity. Basically you need to grab any number of frames from the video using whatever video editing tool you have available, tweak them using your favorite image editing program (like Macromedia Fireworks), and then import them into your FLA file. Create a movie clip that contains the imported images and then use this movie clip within your application.
Take a look at the example file FlashLite_fake_video.fla for more information (see Figure 2). Keep in mind that there's a limited amount of memory available to supported handsets, so try to keep the image sizes small to avoid any memory issues with the Flash Lite 1.1 player.
Figure 2. Image from the Fake Video sample file
Being able to load variables and SWF files into your Flash application is pretty cool, but how do you know when the file you requested has been completely downloaded, let alone successfully downloaded?
This is a common question for developers and is especially important when developing content for mobile phones. Here's my solution to the problem: Use the FSCommand2
("GetNetworkRequestStatus") command, a loop, and a conditional statement.
Interested in seeing how it works? Take a look at the FL_detect_completion.fla file located in the examples folder (see Figure 3). The FLA file has comments within the ActionScript code to help explain what's going on.
Figure 3. Download status image from the FL_detect_completion.fla sample file
There are certain platform capabilities and variables available to Flash Lite 1.1 that you can use to test what functions a specific handset platform supports. Why would you want to know this? Well, if you want to create an application that lets a user dial a telephone number from within a Flash application, for example, you first need to determine whether or not your target handset platform is capable of doing so.
There are two ways of determining this. The first is to copy the capabilities.swf file—included in the Macromedia Flash Lite 1.1 CDK sample files—to your handset. When you run it you get a listing of all the Flash Lite 1.1 platform capabilities. The ones that the handset supports show a result of "1". The other way is to add only the platform capabilities you're interested in using into your FLA file and test it. Either way you get the same results.