Ok...are you familiar with vectors? Dont worry if you are not because even my knowledge is pretty limited when it comes to them. I do however know that a vector consists of a direction (angle) and a magnitude (velocity). Now there is really no point in me going into a full tutorial on vectors or the notation in which vectors should be written in because believe it or not that previous sentence is the only thing you need to know about vectors. Look at this picture for a simple illustration of a vector:
Code:
/
/ velocity
/
/
/ theta
Now of course you need an end point and beginning for that but let's not really worry about anything like that. So the velocity is the length of that line and the direction is "theta" which is the angle that diagonal line makes with the x-axis. Well many physics and math papers and text books you read will deal with vectors so it is good to know about them...however in Flash I like to work with individual x- and y-velocities instead. So we need to find a way of translating the vector into seperate axes velocities. Let's imagine our vector as a right triangle:
Code:
/|
/ |
/ |
vel / | y-vel
/ |
/ |
/ |
/ theta__|
x-vel
And in addition to that lets take the sine and cosine of theta. Just in case you do not know the sine of an angle is the ration of an opposite side to the angle divided by the hypotenuse of a right triangle. So the sine of theta above would be y-vel divided by vel. And the cosine of an angle is the adjacent side divided by the hypotenuse...so x-vel divided by vel. So lets take the sine and cosine of theta:
Code:
x-vel
cos (theta) = -------
vel
y-vel
sin (theta) = -------
vel
Now all we have to do is use simple Algebra to solve for "x-vel" and "y-vel" by multiplying both sides of the equations by "vel"...so the final equations are:
Code:
x-vel = vel * cos (theta)
y-vel = vel * sin (theta)
There you go!...the equations needed to translate from vectors to x- and y-velocities.
So now how to apply to your game? Well first of all you know that when your left, and right keys are pressed they are going to effect the rotation so let's add that first. Oh yeah and before I go on...I just wanted to add that I have never done this before so Im kind of making this up as I go...Im actually making the game as I go through this so I will have a sample for you at the end. Well this is the script I have so far:
Code:
onClipEvent (load) {
rotSpeed = 3;
}
onClipEvent (enterFrame) {
if (Key.isDown (Key.LEFT)) {
this._rotation -= rotSpeed;
} else if (Key.isDown (Key.RIGHT)) {
this._rotation += rotSpeed;
}
}
No big deal right? I have a onClipEvent (load) action to initialize the speed at which I want the ship to rotate when a key is pressed. Then the main part of the script checks to see if on of the keys have been pressed and then rotates the ship accordingly. And just in case you dont know this: x += y is the same as this: x = x + y ...its just a shorter, cleaner way of doing it and also a personal prefrence.
So lets move on to the interesting stuff. We know that when the arrow up key is press we want to add to the current velocity and vice versa for the down arrow. So lets add a script to do that (this is without all the previous script):
Code:
onClipEvent (load) {
incVel = 2;
}
onClipEvent (enterFrame) {
if (Key.isDown (Key.UP)) {
vel += incVel;
} else if (Key.isDown (Key.DOWN)) {
vel -= incVel;
}
}
Make sure to remember that just because Im not putting the whole script everytime that doesnt mean we wont have it at the end...once we are done we are going to compile everything into one script.
So that above script is actually pretty simple. Once again the onClipEvent (load) command initializes a variable, this time used to control how fast the velocity will increase when holding down a key. Then a few simple checks and you add the current velocity with the increment velocity variable or subtract depending on which key is pressed.
Well it looks like we have all the information we need...a velocity and a direction (angle, rotation, direction whatever)...all we have to do is translate it into x- and y-velocities.