Creating a Key Helper Tool
In this section you'll create a handy little helper tool that, once finished, will be accessible from Director's Xtras menu. You can then use the tool to quickly find key codes for keys such as the cursor keys, and any others.This is a very simple tool, with only about five lines of code, but it's quite useful. Also, it also serves to demonstrate the new Tool window type, available in the movie's Display Template.
|1. ||Open key_helper_start.dir from the Lesson12\media folder on the CD.The interface has already been laid out, and a loop on frame behavior has been placed in the Score's behavior channel. To complete the tool you will need to add a keyDown handler to the frame behavior so that you can place the key data into the field sprites.|
|2. ||Double-click the frame behavior at frame 5, in the Score, and add the keyDown handler as shown:|
on keyDown me
member("key_value").text = _key.key
member("keyCode_value").text = string(_key.keyCode)
member("ASCII_value").text = string(charToNum(_key.key))
The three lines of code in the handler place the relevant key information into the three field sprites so that each time you press a key, the information is updated. Note that in the last line a new function, charToNum, is introduced. As its name implies, it takes a character as input, and returns a number. That number is the ASCII code for the character, and can be helpful in certain situations. For instance, when using the keyCode for a key it's impossible to tell the difference between an uppercase and lowercase letter. The keyCode for both 'a' and 'A' is 0. However, the ASCII value of 'a' is 97, and 'A' is 65. In case you need to differentiate between the two, this is one way you can do it.
|3. ||Play the movie and press various keys on your keyboard.If nothing happens, be sure to click on the Stage to give it the focus. Now as you type the key data appears in the fields as expected.|
|4. ||Stop the movie.If you were to now play the movie again, the fields would display their old data until a key was pressed. You should attach a simple behavior to each field sprite so that it is cleared on beginSprite.|
|5. ||Right-click the top field, sprite 7, and select Script from the context menu. Replace the default mouseUp handler with the following beginSprite handler:|
on beginSprite me
sprite(me.spriteNum).member.text = "
|6. ||Name the script clear_field and close the script window. Attach this script to the other two fields, sprites 8 and 9. Play the movie.Now the fields all appear blank when the movie is first played and don't display left overs.Because this movie will be played as a MIAW and also as a tool within the authoring environment, you need to create one more handler. The closeWindow event is sent to a MIAW, and in fact any window, when the window is closed. This works just like the stopMovie handler except that stopMovie isn't called for MIAWs when the window's close button is used. You can take advantage of this event by providing a closeWindow handler that will forget the MIAW when the window's close button is pressed. This way, if you open the tool and then close it, it will be removed from memory.|
|7. ||Stop the movie and double-click the Main Script, in member position 1. Add the closeWindow handler to the script:|
Here again, you make use of the activeWindow property. Because the MIAW is the active window whenever it has the focus, it will be forgotten (cleared from memory) whenever the closeWindow handler is executed.With everything in place, your new helper is nearly complete. You now need to modify the movie's display template so it appears as a tool window when opened, and can be docked with the other tools in the right docking channel.
|8. ||Click in a blank area of the Stage, or the Score, to change the context of the Property inspector. Select the Display Template tab and change the options as shown:You can make the title anything you like, though Key Helper is appropriate. Changing the window's type to Tool allows the window to be docked with other tool windows, as well as docked into the docking channels. In addition to changing the window's type to Tool, you also need to check the Docking option. Make sure that resizable is turned off since you don't want to be able to resize the window.The tool now needs to be placed into the Xtras folder, so can be picked from Director's Xtras menu. Any movie files found in this folder when Director is first started will appear on the Xtras menu.|
|9. ||Save the movie to Director's Xtras folder, under the filename key_helper.In Windows, the path to the XTRas folder should be:|
C:\Program Files\Macromedia\Director MX 2004\Configuration\Xtras
On the Macintosh, the path will be:
Volume Name/Applications/Macromedia Director MX 2004/Configuration/Xtras
Because Director only looks to this folder when first started, you'll need to quit and relaunch it in order for the Key Helper movie to appear in the Xtras menu.
|10. ||Close Director and then relaunch it. Choose key_helper from the Xtras menu.The key helper appears and looks like other Director tools. Note that because the window has a window type of Tool, it features a header bar containing the title, a gripper for docking and undocking, and an expander arrow to collapse and expand the panel.|
|11. ||Place the cursor over the gripper; it will become a four-way arrow. Click and drag the key helper tool by the gripper and drop it into the right docking channel, where the Property inspector is.The helper tool is now docked into the channel, with the other tools. You can leave it docked there, and it will appear each time Director starts.Note that you need to click on the tool to give it the focus, before it will respond to key presses.Now that you have some understanding of keyboard handling in Director, let's move on to adding the player's ship to the game.|