Your Ad Here

December 20, 2009

Moving objects with keys

Name : Moving objects with keyboard keys in Macromedia Flash
Category : Intermediate

In Flash we can move symbols and movie clips by keys. These keys can be any of the keys from the keyboard, they can be other than the arrow keys.

1- First open Macromedia Flash and create a new file.

2- Now create any instance and convert it to a movie clip, by selecting the whole shape and right click on it and select Convert to symbol, choose Movie clip and name it as “Ball”.












3- Now select the ball movie clip and open up the Actions Panel.


4- Write the Actions as follows :

onClipEvent (enterFrame) {
if (Key.isDown (Key.LEFT))
_x --; //to move the object to left i.e. on negative x-axis
}
onClipEvent (enterFrame) {
if (Key.isDown (Key.RIGHT))
_x ++;
}
onClipEvent (enterFrame) {
if (Key.isDown (Key.UP))
_y --;
}
onClipEvent (enterFrame) {
if (Key.isDown (Key.DOWN))
_y ++;
}

5- Description :

    “ onClipEvent (enterFrame) “ =>>

- When the movie enters this frame the action written below will struct

    “if (Key.isDown (Key.any key)) “ =>>

- Key.isDown : when key is pressed
- Key.any key : it can be any key, the action will be performed when this key will be pressed

6- All done now test the movie clip by pressing Ctrl + Enter. The instance will move by the keys written in the script, right now which are RIGHT,LEFT,UP,DOWN keys (Arrow keys).

No comments:

Post a Comment

Please use sensible language.....!