0

   

Moving sprites using AS2 in Flash

This tutorial shows you how to use simple actionscripts to move your sprites using the left and right arrow keys. This post is a continuation from “how to animate in Flash 8” and “Adding motion tween+motion guide to sprites“. It’s best to read them before continuing with this post.

 

Use arrow keys to move.

 

Here are the summary of steps

1) Create ‘stop’ action movieclip

2) Create ‘running’ movieclip

3) Combine movieclips

4) Insert actionscripts

1- Create ‘stop’ movieclip

Create this movieclip which contains the character’s chillin’ out/standing animation. (The steps to do this is to cut out the sprites, place them in frames, and paste the frames into the movieclip).

2- Create ‘running’ movieclip

Then, create another movieclip, this one containing the character’s running animation.

3- Combine movieclips

Now, we are going to ‘combine’ both movieclips into one movieclip. First, convert the ‘stop’ movieclip into a movieclip object again. Do it by clicking on the movieclip, then press F8, and click ‘OK’.

insert_frame_to_2

Then, cut the ‘running’ movieclip by clicking on it, then pressing Ctrl+X (cut). Double click into the movieclip containing the ‘stop’ movieclip, then press F7 to insert a blank keyframe into it’s timeline.

paste

On the movieclip’s stage, right clicking and select ‘Paste’ to paste the ‘running’ movieclip into the 2nd frame. Use Onion Skin view to ensure the ‘running’ movieclip and ‘stop’ movieclip is at the same position. If not, move the ‘running’ movieclip to the proper position.

4- Insert actionscripts

Now, select the first frame. Then, in the actions panel below, type in

stop();

 

first_frame_stop

This script stops the movieclip’s animation on that frame.

Then, double click somewhere on the stage to ‘get out’ of the movieclip. Select the movieclip, and insert the following script:

onClipEvent (load) {
 var speed = 10;
}
onClipEvent (enterFrame) {
 if (Key.isDown(Key.LEFT)) {
 gotoAndStop(2);
 _xscale=-100
 _x -= speed;
 }
 if (Key.isDown(Key.RIGHT)) {
 gotoAndStop(2);
 _xscale=100
 _x += speed;

 }
}
onClipEvent (keyUp) {
 gotoAndStop(1);
}
sakura_script

Here are some explanations for the script:

var speed

Defines your character’s speed

if (Key.isDown (Key.LEFT)){

This detect’s if the left key is pressed. If the left key is pressed,

gotoAndStop(2);

The commands stops the movieclip at frame 2, which contains the running animation.

_xscale=-100 / _xscale=100

This command ‘flips’ the movieclip horizontally. _xscale =-100 means it is completely flipped over, while _xscale=100 turns the movieclip to it’s original state.

_x-=speed

The movieclip moves left, as the smaller the x value, the closer to the left it is, and vice versa.

Conclusion

That it! more updates will be coming! have fun.

prev/next«Adding motion tween+motion guide to sprites in Flash