How to Write a Left Arrow Key in ActionScript 3.0

Writing ActionScript code that will remember which keys on the keyboard have been pressed lets you build creativity and productivity into your Flash movies. Once the key codes for the keys pressed have been written to memory, you can recall them to repeat the ActionScript functions called with the keys. This means that you can replay every action taken when you or a user plays one of your flash games. Writing key presses -- such as a left arrow key press to memory -- requires that you know the left arrow's key code and be able to store it in memory.

Things You'll Need

  • Adobe Flash Professional (CS3, CS4 or CS5 versions)
Show More

Instructions

    • 1

      Start the Flash program. Click "New" from the "File" menu on the main Flash menu bar. Click the "Flash File(ActionScript 3.0)" file type selection from the "New Document" dialog box that appears. Click the "OK" button on the lower right-hand side of this dialog box to close the dialog box and return to the Flash workspace.

    • 2

      Select the "Actions" option from the "Window" menu on the main Flash menu bar to open the "Actions" editor. Position your mouse cursor on the first line of the "Actions" editor. Click your left mouse button to position the cursor on the first line so you can type in ActionScript 3 code.

    • 3

      Type the code to declare a Boolean variable -- a variable that can only be set to a value of true or false -- named "leftArrowSelected." Type the ActionScript code statement "var leftArrowSelected: Boolean" on the first line in the "Actions" editor. Press the "Enter" key on your keyboard to move the mouse cursor to the second line in the "Actions" editor.

    • 4

      Type the code that will create a "KEY_DOWN" event listener that will detect if a key has been pressed. Name the function in the event listener code that the event listener calls as "keyDown_fn." Type this ActionScript event listener code as "stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown_fn)" on the second line of the "Actions" editor. Press the "Enter" key on your keyboard to move the mouse cursor to the third line in the "Actions" editor.

    • 5

      Type the code listed below in the "Actions" editor to display a message if the left arrow key has been pressed:

      function keyDown_fn(event:KeyboardEvent) {

      if (event.keyCode == 37){

      leftArrowSelected=true; } else leftArrowSelected = false;

      if (leftArrowSelected == true) {var storeLeftKeyCode:int = 37; trace("left arrow has been written to memory as keyCode ", storeLeftKeyCode)}

      }

    • 6

      Review the code you typed in and compare it against the working code below. Correct any typing errors if needed. Copy and paste the following code if you want to ensure there are no errors:

      var leftArrowSelected: Boolean

      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown_fn)

      function keyDown_fn(event:KeyboardEvent) {

      if (event.keyCode == 37){

      leftArrowSelected=true; } else leftArrowSelected = false;

      if (leftArrowSelected == true) {var storeLeftKeyCode:int = 37; trace("left arrow has been written to memory as keyCode ",storeLeftKeyCode)}

      }

    • 7

      Click the "TestMovie" option in the "Control" menu on the main Flash menu bar. The statement that was typed into the "trace" statement -- "left arrow has been written to memory as keyCode 37" -- appears in the "OUTPUT" display panel when you press the "left arrow" key.

Learnify Hub © www.0685.com All Rights Reserved