Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

Use Keyboard to Move Object with ActionScript 3 (1)

Many interactive Flash Movies allow users to use Keyboard and Mouse to control objects on the stage. This is the most basic requirement of Flash games. In this series of Flash ActionScript tutorial, we use Keyboard to move a ball on the stage of Flash Movie.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

In this Flash ActionScript 3 tutorial, we use Keyboard keys to move a ball around on the stage. The ball will move only when users press on the keys. Many Flash Movies will let users to use Keyboard to move objects on the stage.

The completed Flash Movie of this tutorial is shown as above. Please click the Flash Movie with your mouse and use the Arrow Keys on your Keyboard to test how it works.

Flash ActionScript Codes:

// We need Keybpard control
import flash.events.KeyboardEvent;

// Function to move the ball respond to an arrow key being pressed
function moveBall(event:KeyboardEvent):void {

if (event.keyCode == 39) {
football_mc.x += 2;
} else if (event.keyCode == 37) {
football_mc.x -= 2;
} else if (event.keyCode == 38) {
football_mc.y -= 2;
} else if (event.keyCode == 40) {
football_mc.y += 2;

// display the location of the ball in the Message Box TextField
output_txt.text = "Location of ball: " + Math.round(football_mc.x) + "," + Math.round(football_mc.y);



// Hook up the Keyborad event with the moveBall function
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveBall);

Download Flash Source File:

Flash Source File keyboard-1.fla


We noticed that the ball will move only when we pressed down the arrow keys. The ball will stop moving when we release the keys. In the next Flash ActionScript 3 tutorial, we will allow the ball keep on moving even when we release the arrow keys. The ball will keep on moving until we press another arrow key. Please read the next Flash ctionScript tutorial.