Skip to navigation | Skip to content

Share your knowledge. Make a difference.

Flash CS3 Tutorial

1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic (by 5 people)   Your rating: 1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic

Ranked #192 in Games, #4594 overall

Rated G. (Control what you see)

Flash Tutorial, Keyboard Contols

 

On this page you will find a partial Flash CS3 Tutorial on how to move an object with the arrow keys. We will create a stage event listener and then move a balloon around.

Here are a few links to some Great Flash CS3 Tutorials 

FREE Flash Tutorials

Are you looking to learn more about Flash CS3 and ActionScript, then you need to check out these great FREE Flash CS3 Tutorials.
FrenchSquared.com
My website and hopefully your next destination for Great Flash Tutorials.
Flash Tutorials
Here is a list of the FREE Flash CS3 Tutorials that I have completed so far. I am adding more everyday.
Arrow Keys Tutorial
Here is the complete tutorial with source files.
F2-4Kids.com
Educational Games, Kids online coloring book, This is a website dedicated to the education of Children. It started as a project to help my children develop there computer skill and has turned into F2-4Kids.com

Flash Tutorial on keyboard listeners 

Flash CS3 Tutorial, make an object move

So, here is your Flash CS3 Tutorial on how to make an object move using the Arrow Keys. All code is in ActionScript 3.0.

Balloon

When working in Flash the key board is just as important as the mouse. However, many web sites over look keyboard intractability. If you want your site to stand out, use keyboard controls. If you test the code below you will see that whenever you press a key Flash CS3 Tutorial tells you a key has been press. Thats great, but what if you want to know when a specific key is pressed. Well then you Continue Smileyneed to know the key codes. I have included a list of these codes at keyCodes

Continue Smiley

This sample of code will detect when a key is pressed then if that key was the space bar, it will output that you pressed the space bar.

function hearKey(yourEvent:KeyboardEvent):void{
if (yourEvent.keyCode==32){
trace("you pressed the space bar");
};
};
stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKey);

Make an Object Move (Arrow Keys)
Ok, so are you ready to do something useful with keyboards events. Let us start with a basic element that any flash game needs, the ability to move an object around the stage.

In this Flash CS3 Tutorial I will teach you how to move a Movie Clip around the stage. In future Flash Tutorials, I will teach you how to detect if an object has hit another object but for now lets just work on getting control of an object. You will need the source files for this tutorial. Source Files

In this file you will find three layer. The actions layer which will as always contain the code or ActionScript you are going to use. The balloon layer which in this case has an instance of a hot air balloon name balloon_mc. and he last layer, background which contains a nice sky scene for the balloon to travel in. Some of he work has once again been done for you. The instance of the balloon is properly named balloon_mc. If you try this project on your own remember that all objects must be named in the properties panel for ActionScript to be able to use them. Please add the example code to the first key frame in the actions panel.

function hearKey(yourEvent:KeyboardEvent):void{
if (yourEvent.keyCode==Keyboard.RIGHT){
trace("ballon is moving");
};
};
stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKey);

First we need to create a function to listen for the key strokes. Listeners need to be applied to an object, but this time the object is the stage. So, we will use stage.addEventListener. We have already covered this code so lets continue to the fun stuff. Please add the new code inside the function you just created, see example. If you test your movie at this point you will see the out panel say the balloon is moving when you press the right arrow key. We use the trace feature to test that the function and if statement is working properly.

Now, we need to actually make the balloon move. We are going to do this by telling balloon_mc to move 5 pixels to the right. In ActionScript 3.0 that looks like balloon_mx.x += 5. That means add 5 pixels to the current x position. Please remove the trace tag and add the code in the example. Press control-enter to test the movie. If your code is correct the balloon will move to the right when the right arrow key is pressed.

function hearKey(yourEvent:KeyboardEvent):void{
if (yourEvent.keyCode==Keyboard.RIGHT){
balloon_mc.x+=5
};
};
stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKey);

We are almost done, all that needs to be done is to add an if statement for the other three arrow keys. Remember that in Flash the stage is made up of x and y coordinates. To move an object you will either add or subtract from it current position. That means to go left you will subtract 5 not add. Look closely at the finished code and you will see how the math works. Look closely at the up and down keys, the math it opposite of what you would first think. That is because in Flash 0 is not the center it is the top left corner to Flash 10 is farther towards the bottom of the stage then 5. Therefore, adding makes the object go down.

Congratulations,
You now have a a Flash CS3 Movie with Interactive Arrow Keys.

This is my own RSS Feed, full of Flash CS3 Tutorials 

FREE Flash CS3 Tutorials

So, this is where I update the Flash Tutorials Daily. If I create or find a new Flash Tutorial you will see it in this list.

Loading Fetching RSS feed... please stand by

Flash Tutorial on Utube 

Flash Tutorial Video's

Here are some of the Best Flash Tutorials available on Utube.
YouTube thumbnail
KnowFlash.com - Create A Carto...

Runtime: 6:50 | 203379 views | Comments

YouTube thumbnail
Adobe Flash Tutorial -- Basic ...

Runtime: 9:18 | 157272 views | Comments

YouTube thumbnail
Flash Tutorial : Learn how to ...

Runtime: 30:10 | 51180 views | Comments

YouTube thumbnail
Flash Tutorial Create a Drop d...

Runtime: 24:48 | 140147 views | Comments

YouTube thumbnail
KnowFlash.com - Create A Carto...

Runtime: 13:24 | 42640 views | Comments

YouTube thumbnail
Flash-animation tutorial(for b...

Runtime: 5:03 | 264991 views | Comments

X
FrenchSquared

About FrenchSquared

Gordon French is a Flash Programmer and Flash Designer. Check out My site at frenchsquared.com
F2-4Kids.com.com

FrenchSquared's Pages

See all of FrenchSquared's pages