This is my first as tutorial so bare with me in places it may be a little bit messy and unclear. I hope you can use this tutorial for a game or similar. So here goes:
You will need to know
AS: _x and _y by True Darkness
AS: ClipEvents by inglor
AS: Movement - Basic
AS: triginometry by BleeBlap
First of all you need to create your car movieclip. Now open the actions panel and give the car movieclip this code:
nClipEvent (load) {
//listen for key presses
downCapture = new Object();
downCapture.onKeyDown = function () {
switch (Key.getCode()) {
case Key.LEFT: left = true; break;
case Key.RIGHT: right = true; break;
case Key.UP: up = true; break;
case Key.DOWN: down = true; break;
}
}
Key.addListener(downCapture);
//listen for key returns
upCapture = new Object();
upCapture.onKeyUp = function () {
switch (Key.getCode()) {
case Key.LEFT: left = false; break;
case Key.RIGHT: right = false; break;
case Key.UP: up = false; break;
case Key.DOWN: down = false; break;
}
};
Key.addListener(upCapture);
rotateBy = 9; //how much we should rotate every frame (L/R key is pressed)
accelleration = 5; //how much we should move every frame (when Up key is pressed)
function radtodeg(deg) { //convert radians to degrees (for later)
return (deg/180) * Math.PI;
}
}
onClipEvent (enterFrame) {
if (left) { _rotation -= rotateBy } //rotate counter-clockwise
if (right) { _rotation += rotateBy } //rotate clockwise
if (up) {
if (accelleration < 10) { //if is below accelleration limit then allow to accellerate
accelleration++;
}
//calculate where the car will move to:
angle = _rotation;
angle = radtodeg(angle-90);
by_x = accelleration * Math.cos(angle)
by_y = accelleration * Math.sin(angle)
_x +=by_x //move the car here
_y +=by_y // and here
}
}
This is the code better explained that can easily help you to improve as you can use small extracts from this tutorial:
onClipEvent (load) {
The script inside this is executed ONLY when the movieclip first appears in the movie(not before)
downCapture = new Object();
downCapture.onKeyDown = function () {
switch (Key.getCode()) {
case Key.LEFT: left = true; break;
case Key.RIGHT: right = true; break;
case Key.UP: up = true; break;
case Key.DOWN: down = true; break;
}
}
Key.addListener(downCapture);
This "listens"(finds out) when the player presses down the arrow keys (left, right, up and down) and stores the result. For example when the down key is pressed the variable for down is set to true
upCapture = new Object();
upCapture.onKeyUp = function () {
switch (Key.getCode()) {
case Key.LEFT: left = false; break;
case Key.RIGHT: right = false; break;
case Key.UP: up = false; break;
case Key.DOWN: down = false; break;
}
};
Key.addListener(upCapture);
This "listens"(knows) when any one of the arrow keys (left, right, up and down) are released as some say. For example when the down key is released by the player the down variable is set to false again.
rotateBy = 9;
accelleration = 5;
function radtodeg(deg) {
return (deg/180) * Math.PI;
}
RotateBy is the number of "pixels" the movieclip will rotate when the left or right buttons are pressed.
accelleration is simply the accelleration of the movieclip when the up button is pressed
Function radtodeg(deg) { is used for converting radians to degrees, we need this in order to make the trig functions to work the way we want to later in the script.
onClipEvent (enterFrame) {
this executes the script inside every time the frame is loaded (by default 12 times per second)
if (left) { _rotation -= rotateBy }
if (right) { _rotation += rotateBy }
every time the left button is pressed the movieclip will rotate by rot number of degrees (counter-clockwise)
same for the right button, but the other direction (clockwise)
if (up) {
Executes the script inside whenever the up button is pressed
if (accelleration < 10) {
accelleration++;
}
If accelleration is less than 10 then the acceleration will increase by 1. This prevents the movielip from exceeding a certain speed. change the number 10 to a higher number to allow it to go faster.
angle = _rotation;
angle = radtodeg(angle-90);
by_x = accelleration * Math.cos(angle)
by_y = accelleration * Math.sin(angle)
_x +=by_x
_y +=by_y
This part is simple triginometry, it determines the amount the movieclip should move horizontally (x-axis) and vertically (y-axis) and then moves it according to the result it got.