00:00
00:00

AS: Overhead car

4,617 Views | 21 Replies
New Topic Respond to this Topic

AS: Overhead car 2006-03-04 02:54:42


AS: Main

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.

Response to AS: Overhead car 2006-03-04 03:07:56


seem's good, maybe an exaple.


========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

BBS Signature

Response to AS: Overhead car 2006-03-04 09:43:44


At 3/4/06 03:07 AM, -Vengeance- wrote: seem's good, maybe an exaple.

Yeha it dosent look to bad at all.
Throw an example together =)


|| Portfolio || Facebook || Twitter ||

BBS Signature

Response to AS: Overhead car 2006-03-04 10:05:45


examples:

http://denvish.net/ulf/1141478333_car example.fla - source file
http://denvish.net/ulf/1141478288_car example.php - view sample

Response to AS: Overhead car 2006-03-04 11:54:35


Yeah, noticed the 'o' issue. Your gonna get free bumps :D

Response to AS: Overhead car 2006-03-04 11:57:46


At 3/4/06 11:54 AM, greenbush wrote: Yeah, noticed the 'o' issue. Your gonna get free bumps :D

It's OK, but you know that a pedal in a car controls acceleration, not speed, and there is no sense of that in your coding.


BBS Signature

Response to AS: Overhead car 2006-03-04 12:07:30


horrible tutorial


- Matt, Rustyarcade.com

Response to AS: Overhead car 2006-03-04 12:24:49


You forgot to metion that you didnt even make the code, and that you got it from a tutorial. Also, I had to help you and fix the codes to make it do what you wanted. You didnt make any part of this code whatsoever.

Response to AS: Overhead car 2006-03-04 12:41:32


i made this code but it was you who helped me with the next bit that i couldn't do

Response to AS: Overhead car 2006-03-04 12:43:36


Well if you didnt make the code and you submit it as an AS: "" thread. then shame on you. If your gonna use code ideas from other tutorials then link them. Ive seen a couple of tutorials written this way

Response to AS: Overhead car 2006-03-04 12:50:55


Fix the link, please

Response to AS: Overhead car 2006-03-04 13:22:51


Liar! You are the biggest liar Ih ave even seen. Do not stel wqork and claim it as you own.
Proof that he is a liar http://www.newground../topic.php?id=442775

Response to AS: Overhead car 2006-03-04 14:23:42


At whateva, -ryu- wrote: script for overhead car game so far i have just used a tutorial to get this code:

By this i meant i have just used a bunch of tutorials to get this! It really annoys me but i guess you might as well lock this as everyone thinks it is stolen.

Response to AS: Overhead car 2006-03-06 04:50:53


crapola...
bad tut bad idea most probebly stolen

Response to AS: Overhead car 2006-03-06 06:00:46


tch...tch...tch...trying to educate us on a subject matter your are uneducated in...the irony is ironic in a parradox kinda way


"It isn't that democrats are ignorant. Far from it. it's just that they know so much that just isn't so"

Ronald Reagan

Proud supporter of the Dinosaur Conspiracy Theory

BBS Signature

Response to AS: Overhead car 2006-04-16 09:20:43


Response to AS: Overhead car 2006-04-16 09:53:08


At 4/16/06 09:20 AM, cLoSeD wrote: looks a lot like http://www.pixel2lif.._the_keyboard/page1/


thief!

Yup, stolen right down to the comments. GJ, creator of this thread.


Come join music competitions on Chips Compo and hang on our Discord!

Good artists copy. Great artists get banned from the Audio Portal.

BBS Signature

Response to AS: Overhead car 2006-04-16 12:39:08


At 4/16/06 09:53 AM, johnfn wrote: Yup, stolen right down to the comments. GJ, creator of this thread.

That's just sad.


BBS Signature

Response to AS: Overhead car 2006-04-16 14:22:39


btw.. you forgot to copy the o from onClipEvent...

Response to AS: Overhead car 2006-08-05 04:10:46


this is a good tutorial, for a tank control. cars don't steer anything like this. there is a much better tutorial on flash kit to make reailistic car movment, in like 30 lines opposed to like 45.

summary:
too complex
unrealistic eg. no sence of proper acceleration or turning
if the statments are correct its stolen to

Response to AS: Overhead car 2006-08-05 04:25:09


At 4/16/06 09:20 AM, cLoSeD wrote: looks a lot like http://www.pixel2lif.._the_keyboard/page1/


thief!

lol, pwn'd.

You are a dead-set tard.