I usually do not put code inside frames, but here you will have to, because I didn’t write the code.
The code was taken from “Beginning Flash Game Programming For Dummies”. I have been trying to add the code so that it will stop the car/boat/spaceship from passing through the walls I make, but since there moving to fast, I can’t. If I figure out the code, I will post it, but if you figure the code…well…post it! Thank you.
I hope you enjoy this tutorial.
The code was taken from “Beginning Flash Game Programming For Dummies”.
Car:
Step 1 ~ Draw a car, or anything that moves like a car.
Step 2 ~ Convert it to a movieclip, call it whatever you want. I prefer “player”.
Step 3 ~ Give it the instance name of “car”.
Step 4 ~ Copy and paste this code to the frame the car is in, remember, I said FRAME.
________________________________________________________________
//carVector
//use vector projection to go any speed, any direction
init();
function init(){
car.speed = 0;
//direction is now in degrees
car.dir = 33;
}
car.onEnterFrame = function(){
checkKeys();
turn(car);
move(car);
} // end enterFrame
function turn(sprite){
//use vector projection to get DX and DY
//offset the angle
degrees = sprite.dir -90;
//convert to radians
radians = degrees / 180 * Math.PI;
//get DX and DY (normalized: length is one)
sprite.dx = Math.cos(radians);
sprite.dy = Math.sin(radians);
//compensate for speed
sprite.dx *= sprite.speed;
sprite.dy *= sprite.speed;
} // end turn;
function move(sprite){
//moves any sprite, wrapping around boundaries
//move
sprite._x += sprite.dx;
sprite._y += sprite.dy;
//rotate changed slightly.
sprite._rotation = sprite.dir;
//check boundaries – wrap all directions
if (sprite._x > Stage.width){
sprite._x = 0;
} // end if
if (sprite._x < 0){
sprite._x = Stage.width;
} // end if
if (sprite._y > Stage.height){
sprite._y = 0;
} // end if
if (sprite._y < 0){
sprite._y = Stage.height;
} // end if
} // end move
function checkKeys(){
//check keyboard to move car
if (Key.isDown(Key.UP)){
car.speed++;
if (car.speed > 8){
car.speed = 8;
} // end if
} // end if
if (Key.isDown(Key.DOWN)){
car.speed–;
if (car.speed < -3){
car.speed = -3;
} // end if
} // end if
if (Key.isDown(Key.RIGHT)){
car.dir += 5;
if (car.dir > 360){
car.dir = 0;
} // end if
} // end if
if (Key.isDown(Key.LEFT)){
car.dir -= 5;
if (car.dir < 0){
car.dir = 360;
} // end if
} // end if
} // end checkKeys
________________________________________________________________
If you have any questions, ask me, thanks for reading, and remember to show me what you made with this. Thanks.
Boat:
Step 1 ~ Draw a boat, or anything that moves like a boat.
Step 2 ~ Convert it to a movieclip, call it whatever you want. I prefer “player”.
Step 3 ~ Give it the instance name of “boat”.
Step 4 ~ Copy and paste this code to the frame the boat is in, remember, I said FRAME.
________________________________________________________________________
//boat movement
//simulate a boat with drift friction
init();
function init(){
//create the boat
boat.dx = 0;
boat.dy = 0;
boat.speed = 0;
boat.dir = 0;
boat.drag = .9;
boat.traction = .5;
} // end init
boat.onEnterFrame = function(){
boat.checkKeys();
boat.turn();
boat.move();
} // end if
boat.checkKeys = function(){
//check for left and right arrows
if (Key.isDown(Key.LEFT)){
this.dir -= 10;
if (this.dir < 0){
this.dir = 350;
} // end if
} else if (Key.isDown(Key.RIGHT)){
this.dir += 10;
if (this.dir > 360){
this.dir = 10;
} // end if
} else if (Key.isDown(Key.UP)){
//thrust on up arrow
this.thrustSpeed = 1;
} else {
this.thrustSpeed = 0;
} // end if
} // end checkKeys
boat.turn = function(){
this._rotation = this.dir;
//get new thrust vector
//thrust vector is direction boat is currently pointing
degrees = this.dir
degrees -= 90;
radians = degrees * Math.PI / 180;
//calculate current speed
speed = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
driftDX = Math.cos(radians);
driftDY = Math.sin(radians);
//adjust driftDX for speed BEFORE applying thrust.
driftDX *= speed;
driftDY *= speed;
thrustDX = this.thrustSpeed * Math.cos(radians);
thrustDY = this.thrustSpeed * Math.sin(radians);
//add thrust to dx and dy
this.dx += thrustDX;
this.dy += thrustDY;
//compensate for drag;
this.dx *= this.drag;
this.dy *= this.drag;
} // end turn
boat.move = function(){
//move according to current speed BEFORE applying thrust.
this._x += driftDX * this.traction;
this._y += driftDY * this.traction;
//move again AFTER applying thrust
this._x += this.dx * (1/this.traction);
boat._y += this.dy * (1/this.traction);
//wrap around screen
if (this._x > Stage.width){
this._x = 0;
} // end if
if (this._x < 0){
this._x = Stage.width;
} // end if
if (this._y > Stage.height){
this._y = 0;
} // end if
if (this._y < 0){
this._y = Stage.height;
} // end if
} // end move
________________________________________________________________________
Kind of the same as the car, right? Well, that’s the good part about it.
Spaceship:
Step 1 ~ Draw a spaceship or anything that moves like one.
Step 2 ~ Convert it to a movieclip, call it whatever you want. I prefer “player”.
Step 3 ~ Give it the instance name of “myship”.
Step 4 ~ Copy and paste this code to the frame the spaceship is in, remember, I said FRAME.
________________________________________________________________________
//ship movement
//simulate a space ship no friction
init();
function init(){
//normal initialization
myShip.dx = 0;
myShip.dy = 0;
myShip.speed = 0;
myShip.dir = 0;
myShip.gotoAndStop(“still”);
} // end init
myShip.onEnterFrame = function(){
myShip.checkKeys();
myShip.turn();
myShip.move();
} // end if
myShip.checkKeys = function(){
//check for left and right arrows
if (Key.isDown(Key.LEFT)){
this.dir -= 10;
this.gotoAndStop(“left”);
if (this.dir < 0){
this.dir = 350;
} // end if
} else if (Key.isDown(Key.RIGHT)){
this.dir += 10;
this.gotoAndStop(“right”);
if (this.dir > 360){
this.dir = 10;
} // end if
} else if (Key.isDown(Key.UP)){
//thrust on up arrow
this.thrustSpeed = 1;
this.gotoAndStop(“thrust”);
} else {
this.thrustSpeed = 0;
this.gotoAndStop(“still”);
} // end if
} // end checkKeys
myShip.turn = function(){
this._rotation = this.dir;
//trace(“I’m here…”);
//get new thrust vector
degrees = this.dir
degrees -= 90;
radians = degrees * Math.PI / 180;
thrustDX = this.thrustSpeed * Math.cos(radians);
thrustDY = this.thrustSpeed * Math.sin(radians);
//add thrust to dx and dy
this.dx += thrustDX;
this.dy += thrustDY;
} // end turn
myShip.move = function(){
this._x += this.dx;
this._y += this.dy;
//wrap around screen
if (this._x > Stage.width){
this._x = 0;
} // end if
if (this._x < 0){
this._x = Stage.width;
} // end if
if (this._y > Stage.height){
this._y = 0;
} // end if
if (this._y < 0){
this._y = Stage.height;
} // end if
} // end move
________________________________________________________________________
Step 5 ~ Inside the spaceship (double click it), label the first frame, “still”.
Step 6 ~ Make another frame, label it, “right”, and draw the ship the way you want it to look when its moving right…Don’t rotate it, the code does that for you.
Step 7 ~ Make another frame, label it, “left”, and draw the ship the way you want it to look when its moving right…Remember, don’t rotate it.
Step 8 ~ Make yet another frame, label it, “thrust”. Draw the ship the way you want it to look when its moving forward. Remember not to rotate it.
The space ship was a bit’ harder and longer to make. But the ship is also very unique, and fun to drive it. Now, go on! Make a game with this! Remember to tell me what you made with this tutorial.
Any questions you have, ask me, I will help…if…well…if I can. Bye!
WARNING:
I fixed the problem, it should all work now.
March 11, 2008 at 11:06 pm
[...] VehicleMotion Car, Boat, Spaceship [...]
March 11, 2008 at 11:39 pm
I found myself in there…What is that? Who’s website is that? Thanks.
March 12, 2008 at 12:28 am
TESTING>…..TESTING!
March 12, 2008 at 12:49 pm
[...] this blog would take a bit’ of there time to leave comments…I can see they like the vehicle motion tutorial, good for [...]
March 28, 2008 at 28:20 pm
is this flash 8 coz i put the car one in and it doent do anything.
but thanks for posting this stuff it realy is useful.
March 28, 2008 at 28:40 pm
All my tutorials so far are Flash 8 Pro.
Make sure you put the code in the frame the car is in, if it doesn’t work, I could post the source file on here. Thanks for that great comment! I love comments…hehe..thanks!
April 2, 2008 at 02:19 pm
Oh, I use flash 7…shame
April 2, 2008 at 02:20 pm
It should work for you, unless you use AS 1.0.
There was a small glitch on there, so I fixed it.
May 22, 2008 at 22:36 pm
hey i have that book too!
though i already know more than half of it…
May 22, 2008 at 22:37 pm
im using flash cs3 with AS 2.0 i put in the car script but it keeps coming up with a syntax error on line 70 (car.speed-;)under were it says if (Key.isDown(Key.DOWN)){ please help me
May 22, 2008 at 22:34 pm
Try copying and pasting it again. And you can also try sending me the .fla. (birdvew@live.com)
Bye bye!
May 27, 2008 at 27:44 am
Okay about the vehicles moving too fast, you are should look up tunneling. This can be solved by using raycasting, or capsule sweep testing
May 28, 2008 at 28:05 am
@tom : syntax error on line 70 (car.speed-;)
—
also got that one too..
changed to (car.speed–;)
but im not really sure bout it.. but it work.. at least no error.
June 1, 2008 at 01:03 pm
lol, thanks.
June 9, 2008 at 09:00 pm
This totally rocked my socks!
June 9, 2008 at 09:02 pm
Oh! I forgot! How do we show you what we made? Thanks a ton.
June 10, 2008 at 10:52 pm
Hi,
the blog has moved to eaglevision.890m.com/blog
You can email me what you’ve made. My email can be found at eaglevision.890m.com/email
thanks!
June 10, 2008 at 10:08 pm
line 73 has an error. change it to car.speed–;
thats car.speed(dash)(dash);