Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

macromedia flash mx - Reinhardt R.

Reinhardt R., Lott J macromedia flash mx - John Wiley & Sons, 2004. - 987 p.
ISBN 0-7645-4354-7
Download (direct link): macromediaflash2004.pdf
Previous << 1 .. 228 229 230 231 232 233 < 234 > 235 236 237 238 239 240 .. 419 >> Next

Key.addListener(oComboKey);
Chapter 19 The Mouse and Key Classes
495
On the Before you add this code to a Flash movie, open the starter file, keyEvent_combo_
CD-ROM starter.fla, in the ch19 folder of the book's CD-ROM. This document contains the artwork for the Find dialog box. This artwork will be attached with the code you're about to add. Note that the findWindowClip symbol has a Button instance that uses a
on(release, keyPress "<Enter>") handler as well.
To see this code in action, do the following:
1. Open the keyEvent_combo_starter.fla document from the CD-ROM.
2. Create a new layer named actions, and place it at the top of the layer stack.
3. Select frame 1 of the actions layer, and open the Actions panel. Type the code shown in Listing 19-6.
4. Save your Flash document as keyEvent_combo_100.fla and test the movie (Ctrl+Enter or ^+Enter). With focus on the Test Movie window (that is, click the Flash movie stage to make sure it is active), press Ctrl+S or ^+S. The Find dialog box appears, as shown in Figure 19-12. Type a search term into the dialog box, and press the Search button. A Web page will appear in a separate browser window with the search results.
? keyEvent combo l OO.swf
To perform a search, press Ctrl+S (Windows) or 3+S (Macintosh)
Type a word or phrase that y. J you would like to search for: V 1-1 I Search | | Cancel |

Figure 19-12: The Find dialog box appears when the key combo is pressed.
You can modify the keyDetect() function to work with different keys or even add more keys to the if() statement. For example, you can add another Key.isDown(), specifying the Key.ALT constant.
On the You can find the completed file, keyEvent_combo_100.fla, in the ch19 folder of the
CD-ROM book's CD-ROM. We also created a keyEvent_combo_focus.html document that uses a JavaScript focus() function to automatically focus the Flash movie in the browser window. The JavaScript focus() method works only for Flash movies viewed in Internet Explorer for Windows.

496
Part V The Movie Classes
Playing a Flash Animation with a Keypress
In this next example, you use the right arrow key to control the playback of a Flash movie. Specifically, you use the right keys to play the animation forward. Once more, the moon and earth instances are your central elements. With each right arrow keypress, your ActionScript instructs the main timeline to play until the next frame label. At this point, acquaint yourself with the prebuilt starter file.
On the You can find this section's starter file, key_playback_starter.fla, in the ch19 folder of
CD-RW the book's CD-ROM.
After you have located the starter file on the CD-ROM, copy it to your hard drive, and open the local copy in Flash MX 2004. The starter Flash document has a motion-guided Graphic symbol of the moon nested within a mask layer on the main timeline. As you can see in Figure 19-13, there are four stages (each with a separate stop() action), representing different positions of the moon as it orbits the earth.
t key playback starter.fie .0
Scene 1 |. 4>. 100% | at ll
Timeline
? i S 10 | 20 25 0 36 40 46 50 55 60
U? labels ? O jstagejO ?>stage_3 ,n
LJE? 9 ? ?*5t0p gb^StQD Do#StP *
LJ mask a ? 0 a
[0 moonGuded a . D
[J mcEarth a
?*?] ? [: is 15.0 fps 0.9s < >i
Figure 19-13: The composition of the starter file's main timeline
Chapter 19 The Mouse and Key Classes 497
The frame labels start the numbering scheme with 0, so that they correspond with array values that are introduced in a later example. Basically, you want to enable the right arrow key to issue a simple play() command to the main timeline. The timeline automatically stops at the next frame label because there are stop() actions at each frame label. After stage_3, a
this.gotoAndStop("stage_0"); action resets the timeline.
The first frame of the main timeline is reserved for actions that should be played (or invoked) only once in the Flash movie. With this specific example, you add a Key l istener that should not be specified with the addListener() method more than once. If you add the same listener object to a listener-enabled class more than once, the methods of the listener object will be executed in multiples when the event(s) associated with the listener class are detected.
For more information on arrays and indexes, refer to Chapter 11.
Following is your pseudo-code for this example:
when a key is pressed
if the key is the Right arrow key then advance to the next frame label end if end key capture
Translate this pseudo-code into the proper ActionScript syntax.
1. Create a new layer and name it listener actions.
2. With frame 1 of the listener actions layer selected, open the Actions panel (F9). Your translated actions are as follows:
var oKeyDetect:Object = { owner: this }; oKeyDetect.onKeyDown = function():Void { if (Key.getCode() == Key.RIGHT){ this.owner.play();
}
};
Key.addListener(oKeyDetect);
Previous << 1 .. 228 229 230 231 232 233 < 234 > 235 236 237 238 239 240 .. 419 >> Next