FLASH LITE 1.1 AND YOUR FIRST FLASH LITE APPLICATION

Một phần của tài liệu vnz 0313 foundation flash applications for mobile devices (2006) (Trang 66 - 120)

mobile devices such as cell phones and PDAs. The desktop Flash player is ubiquitous—it reached 98 percent browser penetration back in 2004. In the mobile industry, we don’t yet see these sorts of figures, but Adobe is taking every measure to ensure we don’t have to wait around too long before we reach a critical mass, so we can be assured of mass market penetration of our Flash Lite applications very soon. This chapter contains Flash setup information and a lot of general Flash and ActionScript theory. If you are already comfortable with a version of ActionScript, you might prefer to skim through and pick out the bits that appear unfamiliar. In this chapter, I cover the following topics:

Setting up your Flash Lite studio

Obtaining a Flash Lite player for your device Creating a simple test application

A Flash Lite 1.1 ActionScript primer, including variables, operators, and control structures Coding conventions

FSCommand2commands Useful functions

Limitations of ActionScript for Flash Lite Version inconsistencies and pitfalls SWF tools

The Flash Lite 1.1 Content Development Kit (CDK)

Without further ado, let’s head straight into it and create some Flash Lite content.

Setting up your Flash Lite studio

If you are using Flash MX 2004, this section is for you. If you are using Flash 8 Professional or Flash 9 Professional, you can skip the subsequent “Installation” section if you wish, as these are already set up for Flash Lite development. Beginning Flash Lite development has the following prerequisites:

Flash MX Professional 2004 or later: Version 7.0.1 or greater is required; 7.2 or Flash 8 Pro is recommended.

The Flash Lite player for the Flash IDE: FlashLite1_1.dll for PC, or FlashLite1_1.dmg for Mac.

Flash Lite 1.1 publish profile configuration file: FlashLite1_1.xml.

The device’s configuration file: DevicesMsg.cfg.

Device templates: Flash-based “emulators” for different mobile devices allowing you to easily test your applications.

The Flash Lite player, publish profile configuration file, and device configuration file are included as part of the Flash Lite 1.1 CDK, which is available for download at the following URL:

www.macromedia.com/devnet/devices/flashlite.html

In addition to the files listed previously, you may also like to download the sample files provided on this book’s web site at www.flashmobileforum.org/samples to follow the examples provided throughout the book.

Now that you know what you need, let’s run through the installation process for Flash Lite using the MX 2004 versions of Flash.

Flash MX 2004 Professional

The Flash MX 2004 IDE provides you with everything you need to begin developing Flash content, from design tools to a full-featured code editor. If you have purchased the Flash IDE already, I strongly suggest that you check which version you are running from the Help➤About Flash Professionalmenu option. The minimum required version to author Flash Lite content is 7.0.1, although 7.2 is strongly recommended, as it provides a whole host of improvements to the IDE. If you are running a version of MX 2004 prior to 7.2, you can upgrade to the latest version at

www.macromedia.com/support/flash/downloads.html

At this web site, you can download an updater application for PC or Macintosh that fixes several bugs and problems associated with the previous releases of the IDE, components, and documentation, as well as providing many other noticeable improvements to stability and workflow. This update is free to all users with a valid license and does not alter any projects you have created with a previous version.

The Flash Lite player for the Flash IDE

The Flash Lite player DDL or DMG file provides the Flash IDE with the ability to play back and test Flash Lite 1.1–exported content. It also displays any warnings and error messages associated with Flash Lite development and the many new commands available. To install this file, simply copy FlashLite1_1.dll (or FlashLite1_1on a Mac) to one of the following hard disk locations:

Windows:C:\Program Files\Macromedia\Flash MX 2004\language\Configuration\players Macintosh: Macintosh HD::Applications:Macromedia Flash MX 2004:Configuration:players

The Flash Lite 1.1 publish profile configuration file

When starting the Flash IDE, the application looks in the aforementioned Playersfolder for XML-based publish profile configuration files to determine which extra profiles and player versions are available for use when exporting your movies. FlashLite1_1.xml tells the Flash IDE to add a new player version to the export settings screen and contains information on which player file to use to test Flash Lite content, as well as adding extra documentation to the Actionspanel to ease development when scripting.

For Windows users, language represents your two- or three-letter language code. For example, en is used for all English language installations, es for Spanish, and so on. For a full listing of language codes, you can refer to the System.capabilities.languagesection in the bundled Flash Help.

If you open up this file in your favorite general text editor (for example, Notepad2 from www.flos-freeware.ch), you notice that it contains, near the end, information on device capabilities.

You can manually alter these values to more accurately emulate your known target device, although this is never as good as actually testing on the device itself. Testing is discussed later in the book; for now, just copy FlashLite1_1.xml into the same folder as the previous player file (FlashLite1_1.dll or FlashLite1_1) and, as before, be careful to enter the correct language code if you are using Windows.

The device’s configuration file

DevicesMsg.cfgdefines the features that are supported when testing your Flash Lite content in the IDE’s Flash Lite player. Again, if you open this file in a text editor, you will notice all of the lines are commented out. Uncommenting these lines enables (or disables depending on the context) features such as support for the four-way navigational pad present on most phones. Most of these options can be switched on by uncommenting a line and setting the variable to on. For example, change the following line:

// GetURLOnePerKey=off to this one

GetURLOnePerKey=on

This setting allows calls to getURL()only when called from a keypress event, not a frame or any other scripted event. All of these configuration options and their meanings can be found in the appendixes at the end of this book.

To complete the installation of the required Flash Lite files for Flash MX 2004, copy DevicesMsg.cfg to one of the following hard disk locations and restart Flash:

Windows 2000 or Windows XP: C:\Documents and Settings\user name\Local Settings\Application Data\Macromedia\Flash MX 2004\language\Configuration\

Windows 98(SE): C:\Windows\Profiles\user name\Application Data\Macromedia\Flash MX 2004\language\Configuration\

Macintosh: Macintosh HD::Users:user name:Library:Application Support:Macromedia:Flash MX 2004:language:Configuration:

user namerefers to the user for which Macromedia Flash MX 2004 is currently installed. In Windows XP, you can find out the name of the currently logged-in user by pressing Ctrl+Alt+Del and clicking the Userstab.

In Flash 8 Professional, DevicesMsg.cfgis ignored and is replaced by a series of XML configuration files for each of the handsets being tested. You can find these files in Flash 8 Professional/<language>/Configuration/Mobile/Devices/.

Device templates

Once you have installed the required Flash Lite authoring files, you may also find it useful to create or download device templates for the devices you wish to develop for. Device templates usually consist of an empty FLA file preset with the Flash Lite 1.1 publish settings, a guide layer containing the device graphic (with a transparent window for the screen), and in certain cases, a guide layer containing device specifications (such as screen size and capabilities). David Mannl, an experienced Flash Lite developer, has created a set of device templates for a wide range of handsets including Nokia, Sony Ericsson, and Siemens. These can be downloaded from the following web site:

www.flash-lite.de/downloads.html

Templates are installed to one of the following locations:

Windows: C:\Program Files\Macromedia\Flash MX 2004\language\Configuration\

Templates\Mobile Devices

Macintosh: Macintosh HD::Applications:Macromedia Flash MX 2004:Configuration:

Templates:Mobile Devices

Placing FLA files in this directory creates new file templates available from the Flash start page or File

➤New dialog. Figure 3-1 shows the start page with the Mobile Devices category highlighted. You can create template categories of your own by adding subfolders to the Templatesfolder and placing your FLA files in those folders.

Figure 3-1. The Flash MX 2004 start page

Figure 3-2 shows the Flash IDE just after opening a file created using the Nokia 7610 template. You can see the two guide layers that display the phone graphic and specifications. Testing the movie does not export either of these layers, so it is perfectly safe to keep one or both of these in the FLA throughout production.

Figure 3-2. The Flash MX 2004 IDE with the Nokia 7610 template

Obtaining the Flash Lite player for your device

Now that your Flash Lite studio setup is complete, you might want to think about getting a Flash Lite player for your handsets and any other devices you may own. The Flash Lite player is available for pur- chase in the software section of the Adobe Store (www.macromedia.com/store).

The price is nominal (around $8 per device), and you can begin testing on real devices right away. Each Flash Lite player you purchase from the store requires you to send Adobe your device’s 15-digit IMEI number. This number uniquely identifies your device and allows the Flash player to be locked for oper- ation on it. On most handsets, you can obtain this number by typing the following into the keypad:

*#06#

Once the purchase is complete, you receive a Flash Lite player installer file, most likely in the form of a Symbian installer application (a SIS file). Transferring and installing this file depends on the handset or device. Most phones now come with software and cables to transfer and install software from a PC or Mac. Installation can take place over a wide variety of mediums, including Bluetooth, USB/serial cable, infrared, e-mail attachment, and over the air (OTA) via a WAP site link. It is always best to con- sult your device’s manual or manufacturer’s web site for specific and full instructions on transferring and installing applications.

As well as purchasing the Flash Lite player for your handsets, I recommend joining the Adobe Mobile Developer Program. Benefits include notifications of events and relevant news, as well as the oppor- tunity to join Flash Lite beta programs, allowing you to obtain the latest Flash Lite player for testing (providing feedback to Adobe is a matter of courtesy, of course). You can join the Mobile Developers Program by e-mailing mobiledeveloper@Adobe.com.

Creating a simple test application

Now, let’s test if everything is working as it should. Fire up Flash MX 2004, and choose the Nokia 7610 template discussed previously. With the Text Toolselected, choose a 14-point, blue, sans-serif font such as Arial or Verdana from the Propertiespanel, or let Flash decide which sans-serif font to use by select- ing _sansfrom the font selection drop-down combo box. Next, simply draw a text field on the stage, and enter the words Hello World. Your screen should now look something like the one in Figure 3-3.

Figure 3-3. The Hello World sample file

You can now test the movie by pressing Ctrl+Enter (or Command+Return on the Mac), or by using the menu system to select Control➤Test Movie. You should see the movie displayed in a new window, and the following text should appear in the Outputpanel:

*** Flash Lite Info - FTPS084: Configuration file found.

*** Flash Lite Info - SWFS028: File size: 0.11 kilobytes.

The codes FTPS084and SWF028represent the various events or errors that can occur when publishing for Flash Lite; a full list of codes and definitions is included in this book’s appendixes. If you are hav- ing trouble following the previous steps, you can download a premade version of the Hello World file (HelloWorld.fla) from the Chapter 3 samples folder from the book’s web site.

A Flash Lite 1.1 ActionScript primer

ActionScript for Flash Lite 1.1 is best described as Flash 4 ActionScript with Flash 5 objects. All this really means is that it is a combination of Flash 4–style code, such as using on(event)handlers and calls to getProperty(), in conjunction with some of the newer Flash 5 built-in objects such as the Mathobject, in order to provide extra functionality. When I refer to “later versions” of ActionScript, I am talking about ActionScript for Flash 5 or later, because a lot of the behavior of ActionScript changed after Flash 4 with the introduction of dot syntax and object-oriented programming. This primer is designed to give you a grounding in ActionScript for Flash Lite, from variables to functions, and no previous ActionScript experience is required.

For most of us, it’s been a while since we’ve worked with Flash 4 ActionScript, if we ever have—

certainly since before the removal of normal mode from the Actionspanel. It’s very interesting to revisit some of the techniques and tricks associated with Flash 4–style coding from a completely dif- ferent perspective. Also, you have several years of Flash 4 programming exploration by the Flash com- munity to fall back on for any problems you encounter. Adobe has made scripting for Flash Lite as easy to use as possible. For example, the compiler allows you to use many of the programming con- structs you are already used to, including some dot syntax, and silently converts it to “old school”

Flash 4 code behind the scenes. If you have no previous Flash or ActionScript experience, you would benefit from reading Foundation Flash 8, mentioned previously, and Object-Oriented ActionScript for Flash 8 by Peter Elst and Todd Yard (friends of ED, 2006.)

For the remainder of the book, I use the terms “compile” and “test movie” inter- changeably to refer to the process of exporting a Flash movie by pressing Ctrl+Enter (or Command+Enter) on the keyboard or using the Control➤Test Moviemenu option.

Please also make sure that you have selected the correct publish profile to begin with by creating your document from a Flash Lite 1.1–specific template, such as the Nokia 710 template used in the Hello World example, or by manually editing the publish version in the settings interface found in the Propertiespanel. A com- mon mistake to make at first is to accidentally use the Flash Lite 1.0 profile (which is the default for all included mobile device templates that ship with Flash MX 2004). Using Flash Lite 1.1–specific features when exporting for Flash Lite 1.0 results in the following message appearing in the output window: WARNING: This movie uses features that are not supported in the Flash Lite 1.0 player.

Variables

Variables in Flash Lite behave in exactly the same way as with other Flash player profiles in as far as they allow you to store values to be read and manipulated at runtime. The following types of values can be stored in variables:

Number String

Boolean: converted to 1or 0at runtime

undefined: cannot be assigned to a variable but is used internally by the player to indicate that a variable has not yet been defined or assigned a value

Unlike in later versions of ActionScript, the use of the keyword varto declare a variable is not sup- ported. You define variables in code simply by assigning them a value. An example of each of the val- ues shown in the previous list can be seen in the following code:

age = 50;

apple = "red apple";

isValid = true;

fruit = apple; // stores "red apple";

trace(banana); // outputs: "undefined"

The fourth line shows how you can read the value contained in another variable at runtime. The final line shows what happens if you try to read the value of a variable that has not previously been assigned a value. Variable names must contain only alphanumeric characters, underscores (_), and dol- lar signs ($) and must not begin with a number.

Operators

Flash Lite supports all of the operators found in Flash 4, including eqand gt, but it also supports some of the functionality found in later versions, such as ==. However, rules govern which operators can and should be used with which data types; we discuss these later in this primer. When talking about the variables or values to the left or right of an operator, I use the term operand; a set of operands and operators together make up an expression.

General operators

In this section, we will look at a couple of general purpose operators that you will be using all the time in your ActionScript coding.

Assignment operator (=) This operator is, perhaps, the simplest to use. The value to the right of this operator is assigned to the variable to the left, for example:

lives = 5;

choice = "one";

theirChoice = choice; // theirChoice stores the value "one"

The final assignment shows a variable to the right of the =operator. When this is the case, the value contained in that variable is assigned to the left-hand side, unless the right-hand variable is a complex object. However, complex objects are not supported in Flash Lite 1.1 ActionScript.

The assignment operator can also be used in a compound form alongside arithmetic operators such as +, -, *, /, and %to perform an assignment operation after performing the operation indicated by the first symbol. This makes for easy-to-read, lean code and is often used to increment variables in code loops. An example of this follows:

num = 10;

num += 10; // num = num + 10 num /= 5; // num = num / 5 trace(num); // outputs: 4

Ternary operator This operator is a compound form of an ifstatement when used for conditional assignment (the ifstatement is discussed further in the section entitled “Control structures”). The fol- lowing code shows a rational way of acting on a condition:

if (vegetarian) { pizza = "margerita";

} else {

pizza = "pepperoni";

}

This code can be rewritten using the conditional operator:

pizza = (vegetarian) ? "margherita" : "pepperoni";

To understand what’s going on, take a look at the following psuedo-code:

variable = (condition) ? ifTrueVal : ifFalseVal;

If conditionis true, variable is assigned the value of ifTrueVal; if not, it is assigned the value of ifFalseVal.

Numeric operators

The numeric operators allow you to compare numbers. It is often tempting to use these operators with other primitives such as strings, as you can in later versions of ActionScript. This can lead to unex- pected behavior in certain circumstances or a failure to function altogether; the reasons are explained fully for each of the operators that follow.

Complex objects can be found in Flash 5 and later, so they are likely to appear in future versions of the Flash Lite player. Complex objects can contain multiple properties, methods, or other objects (known as child objects). An example of a complex object might be a variable named car that contains properties for speedand bhp. Access to these properties could be achieved through the use of the dot operator (.), for example, car.bhp = 297;, or with the array access operators, car["bhp"] = 297;. Arrays and movie clips are also complex objects.

In Flash Lite 1.1, we can mimic this behavior using movie clips and the colon (:) operator (discussed later in this chapter). Arrays, however, are not supported.

Một phần của tài liệu vnz 0313 foundation flash applications for mobile devices (2006) (Trang 66 - 120)

Tải bản đầy đủ (PDF)

(537 trang)