First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
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
(10 being the highest)
» 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!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: doc_chin7
» Description:

The flash's horizontal road drive was done in CS4 and was upgraded to CS6. The car responds to right/ left key and flash will auto-generate the movement of roads from left-right or right-left based on random number. Have fun driving in this virtual environment and avoid other cars... :) If you are creative, a road driving game can be created from it.

Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.