Movie Clip controlling using the action script

14.8.2007, 15:23
Submited in: Flash | Total Views: 60605

In this lesson you will learn how to control movie clip. You will also learn how to use if, else, else if and switch/case command.

Click on the picture and move the movie clip using the arrows key!

Step 1

Create a new flash document.

Step 2

Select Modify > Document (shortcut key: Ctrl+J ). Set the width of your document to 300 pixels and the height to 250 pixels. Select any color as background color and set your Flash movie's frame rate to 36 fps. Then, click ok.

Step 3

Draw, or Import any object, which you like to use for this example.

Step 4

After that, select that object and press F8 key (Convert to Symbol) to convert it into a Movie Clip Symbol.

While the new made Movie Clip is still selected, go to the Properties Panel (Ctrl+F3) below the stage. On the left side, You will find the Instance name input field there. Call this Movie Clip target. See the picture below.

Step 5

Double click on layer 1 to rename its name in object. After that, create a new layer above the layer object and name it action.

Step 6

Click on the first frame of layer action and go to the action script panel (F9). Then, enter this code inside the actions panel:

this.onKeyDown = function() {
var code:Number = Key.getCode();
switch(code) {
case Key.LEFT:
case Key.RIGHT:
case Key.UP:
case Key.DOWN:

} Key.addListener(this);

var SPEED:Number = 5;

function moveLeft():Void {
var leftWall:Number = 0;
if(target._x > leftWall) {
target._x -= SPEED;
}else {
target._x = leftWall;

function moveRight():Void {
var rightWall:Number = Stage.width - target._width;
if(target._x < rightWall) {
target._x += SPEED;
}else {
target._x = rightWall

function moveUp():Void {
var topWall:Number = 0
if(target._y > topWall) {
target._y -= SPEED;
}else {
target._y = topWall;

function moveDown():Void {
var bottomWall:Number = Stage.height - target._height;
if(target._y < bottomWall) {
target._y += SPEED;
}else {
target._y = bottomWall;

A little code explanation:

In this script we have created handler of case onKeyDown for class Key. You have also calculate the edge of scene and you also test are the movie clip is going out of that border. The value of edge are represented by topWall,bottomWall, rightWall and leftWall characteristics. This is very simple example for if/else command using.Every function has specific logic for movie clip moving.Function moveLeft () are moving our movie clip left , moveRight () on the right....

Have a nice day!

Download example

Are you a website owner?
Click here to register and submit your tutorials!

Browse by category

Most popular tutorials