Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Control Objects using your Keyboard (episode 44)

0 (0 Likes / 0 Dislikes)
[Adobe Developer Connection] [Doug Winnie - Principal Product Manager, Flash Catalyst, Flash Platform Workflow] Hello again. Let's introduce another new concept in ActionScript that we haven't covered before, how to work with the keyboard. Working with the keyboard in ActionScript can be daunting for a lot of new people. You have to understand how the keyboard works, and there are certain character codes and key codes that are associated with all the different letters and characters or keys that are on the actual physical keyboard. Using the code snippets takes a lot of the pain out of learning all the different key codes and aspects of working with the keyboard. So, let's just apply a sample code snippet and show you how this works. In this case, the actual action that we're working with is in the Animation section of the code snippets panel. We want to move an object with the keyboard arrows, which you'll see is the first option inside of this category. I have the object selected, so let's go ahead and add that particular code. You'll see that the code is added into the Actions panel. Let's take a look and then walk through each of the lines of code, and I'll explain what is going on. You'll see that we're adding a new EventListener to the stage. This time we're with an event known as the KeyboardEvent. The KeyboardEvent has a property known as KEY_DOWN, and we're executing an event handler, based on when this event is fired. That event is looking for a KeyboardEvent very similar to when we work with the mouse event, but in this case, it's not a mouse. It's the keyboard, so we're working with a keyboard event. Then we're working with this new construct called a switch. A switch is conditional. It is very similar to a if statement, like we've covered in the past. The switch statement has an object at the top that is going to be used as a case. It's going to look for the value called event.keyCode. This is a value that is provided by the keyboard that has a specific number for each key on the keyboard. In ActionScript, every key has a certain number. In this case, we're working with the UP, DOWN, LEFT, and RIGHT. So the key code is going to be mapped to these objects--UP, DOWN, LEFT, and RIGHT. Luckily, since we're working with a code snippet, we don't have to understand all of the intricacies of how this is actually coded to the key code and all of the numbers and values there. It's just created for us here, so we can modify the code to get a desired result. Let's go ahead and run this, and I'll just show you what we have right out of the box. I've run the project, and now I actually am pressing the keyboard arrows on my keyboard here, and you'll see that I am moving the object around. I'm pressing left, and I'm pressing up, now down, and now right. If I want to modify the functionality of this, you'll see that there are, again, instructions like any other code snippet, where I can modify the distance that the object moves with each key press. I can modify this and say I want to move it 10 pixels, not 5, and do this with all of the other ones. Sometimes the comments are very helpful about, as it says, as you'll notice, that the number 5 appears four times in the code below, so the comments and the instructions are pretty clear as to what you need to do. Let's go ahead and run this again. Now, you'll see that the object is moving around a lot faster with each key I press. So even though we don't necessarily understand all of the intricacies of working with the keyboard, the code snippets allow us to take a basic functionality that we all want to have in our projects and can put it inside of the project. Now, that we have a basic understanding of all of the ActionScript that starts this off, you can then explore the documentation and experiment and learn more now that you understand the basics of how this works. If you want to learn more about code snippets and other features of Flash Professional CS5, check out the developer's center or check out the other video tutorials on Adobe TV. [Adobe Developer Connection]

Video Details

Duration: 4 minutes and 31 seconds
Language: English
License: All rights reserved
Genre: None
Views: 88
Posted by: adobetv on Oct 21, 2010

Get more functionality out of your keyboard by applying new concepts in ActionScript. Doug uses the code snippets in Flash Professional CS5 to program arrow keys to control objects.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.