First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Alex Dodge | Website: http://alexdodge.net

Okay, to start out, this is my first tutorial. Bear with me.

This tutorial is about how to disassemble a force, direction vector in to x speed and y speed for moving a symbol. This will be useful if you have a symbol that you want to propel at a specific speed at an angle that is not perfectly along the X or Y-axis.

When you've finished this tutorial, you'll have a little game with a spaceship that is controlled with the keyboard.

All right, thus endith the preambles.

First, start a new flash file. Good job.

Now, draw a little circle with another, small circle to the right of it. This is your spaceship. Turn it in to a movie clip symbol. Name it "ship".

Okay, open up the actions of your new symbol. Put the following code in it:


onClipEvent (load){
dir = 45;
speed = 5;

onClipEvent (enterFrame) {
xspeed = Math.cos(dir*Math.PI/180)*speed;
yspeed = Math.sin(dir*Math.PI/180)*speed;
_x += xspeed;
_y += yspeed;
_rotation = dir;


This, basically, takes the direction and speed, converts it in to x and y speeds with a pretty little bit of trig, then moves the ship by that much.

If you ran it now, you'd see your spaceship zipping off at a forty-five degree angle towards the lower right corner of the screen. Cool huh?

Now, you need to add keyboard inputs, since retyping the variables in the preloader every time you want to turn.

Add the following code to the onClipEvent(enterFrame):


if (Key.isDown(Key.UP)) {
speed += 2;
if (Key.isDown(Key.DOWN)) {
speed += -2;
if (Key.isDown(Key.RIGHT)) {
dir += 15;
if (Key.isDown(Key.LEFT)) {
dir += -15;


This, basically, just lets you change the dir and speed variables with the arrow keys.

If you ran the script now you would have a little zippy spaceship that you could move around. Fun, huh?

Well, that's about it. If you want to know how the vector itself is decomposed, I'll give a quick watered down version:

If you think of the vector as a triangle where you know the angle and the hypotenuse, then you can use sine and cosine to find the other two sides.

Taa daa! Very watered down...

Okay, that's it. Have fun!

» Level Intermediate

Added: 2002-11-02
Rating: 7 Votes: 77
» Author
My details? Hm... All you really need or want to know is on my blog, alexdodge.net...
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.