Free ActionScript

Home

AS3 Scripts

AS2 Scripts

Resources

About

Contact

This is a GTA style top-down view car driving game example. This fla shows you how to implement car movement; Acceleration, Braking, Turning & Reverse. Customizable Car Settings:

Max Speed – max car speed

Max Reverse Speed – max car speed in reverse

Acceleration – acceleration speed

Deceleration – braking speed

Friction – how far the car rolls before stopping

Max Steering Rate – max rate at which car can turn

Steering Acceleration – how fast the car starts turning

Steering Friction- how fast the car corrects itself (when you “let go of the wheel”)

Car Starting Direction – set car rotation on start

Preview

Download Fla Sample

Updates 1/09/2011 – v2.0 -Re-wrote most of the code -Fixed drifting bug -Added car starting rotation

Update: ActionScript 3 version of this script is available here:

http://www.freeactionscript.com/2010/06/as3-car-movement-acceleration-turning-braking/

how come i can’t see the actionscript code, the flash file loads, i wanna try play around with the script, but i cant see it, i have Adobe Flash CS3

KriptoKnight, take a look on the timeline.

Cool, thanks pradvan just found it.

Cheers mate, it really helps with my school ITS assingment(we have make a actionscript 2 game and make a totourial on how to make for the younger grades)

love the dynamics of this code but for the life of me can’t find why the car seems to drift to the top/left when no arrows are pressed. happens when driving around and then stopping or just letting the car stop by not touching the keys. i tested it out in the embed above and it does the same thing too. so.. just curious if you might know what might be the issue. thanks

livingpaint, I had not noticed it before, but it’s definitely difting. I’ll have to take a look and fix it. Thanks for bringing it up! Phil

Pradvan, one thing I noticed, while testing it out and trying to come up with a solution myself, is that if you drive it around and are able to point the car facing down (straight – 6 o’clock) the drifting doesn’t occur. Almost any other degree from that rotation will cause the car to drift. Which might be the code relating to the car correcting itself when the keys are up. Hope this help with the debug.

livingpaint, I finally got around to checking out the script. Adding a minimal speed check fixes the drifting. Just add this code right below the part that says “// friction” in the runGame function:

if(speed < 0.01) { speed = 0 }

Once again, thank you for pointing it out. Phil

Michael December 23, 2010 at 2:24 am Hi, I just wanted to mention that I could not reverse when I put that code in below the part that says “//function”. Log in to Reply Michael December 23, 2010 at 2:25 am Oh, sorry, I meant to type “//friction” Log in to Reply pradvan January 8, 2011 at 11:36 am Thanks for pointing that out Michael! The correct code should look like this: if(speed > 0 && speed < 0.01) { speed = 0 } Log in to Reply

Hi, I just wanted to mention that I could not reverse when I put that code in below the part that says “//function”.

Michael December 23, 2010 at 2:25 am Oh, sorry, I meant to type “//friction” Log in to Reply pradvan January 8, 2011 at 11:36 am Thanks for pointing that out Michael! The correct code should look like this: if(speed > 0 && speed < 0.01) { speed = 0 } Log in to Reply

Oh, sorry, I meant to type “//friction”

Thanks for pointing that out Michael!

The correct code should look like this:

if(speed > 0 && speed < 0.01) { speed = 0 }

The movement is so smooth. I had a great time messing with the script and managed to change it around so it functions exactly the same in Actionscript 3.0.

Would anyone experienced with actionscript consider making this AS2.0 code work in 3.0 tricky or just an easy fix?

I only learned what actioncript was a week ago when I was messing with a trial of Flash Professional CS5 and I never realized that troubleshooting with this crazy computer language would be so fun!

pradvan January 8, 2011 at 11:39 am Glad you’re enjoying AS3! A little while back I made an AS3 version of this script. It’s available here . Log in to Reply

Glad you’re enjoying AS3!

A little while back I made an AS3 version of this script. It’s available here .

I’ve had this little script bookmarked for quite awhile now. It just so happens that today, I need it

However, I just spend about 3 hours pulling it to pieces, trying to figure out how I can get the car to start in the direction I want..

No matter the variables, the rotation of the MC, or anything, the car is flipped around on start, thanks to the radiance var.

Any help would be greatly appreciated, Guil

pradvan January 8, 2011 at 11:42 am Guil, To make the car rotate to the direction that you want, just add degrees to this code: player_mc._rotation = radiance*180/Math.PI; So if your library moving click is pointing up, you want 90 degrees: player_mc._rotation = radiance*180/Math.PI + 90; Log in to Reply

Guil,

To make the car rotate to the direction that you want, just add degrees to this code:

player_mc._rotation = radiance*180/Math.PI;

So if your library moving click is pointing up, you want 90 degrees:

player_mc._rotation = radiance*180/Math.PI + 90;

Please note; I rewrote and optimized most of the code. V2 fixes all the mentioned bugs and adds starting car rotation.

[...] Tutorials Recent Commentspradvan on Car Movement: Acceleration, Braking, Turning & ReverseGet number of days in a month | Free ActionScript | Flash Designers on Get number of days in a [...]

That’s awesome – thanks for the help

Like I said, it’s greatly appreciated! I hope you get that warm fuzzy feeling for helping some random people over the interwebz

You must be logged in to post a comment.

AS3 Effects

AS3 Gadgets

Game Examples AS2 Effects Animated Effects Dynamic Enemy AI Game Engine Game Weapons Images Interface Math Security

Effects Animated

Effects Dynamic

Enemy AI

Game Engine

Game Weapons

Images

Interface

Math

Security

Game Examples AS3 Basics Bitmap Blitting Collision Detection Enemy AI Movement Physics Space Shooter User Interface Weapon System

Basics

Bitmap Blitting

Collision Detection

Movement

Physics

Space Shooter

User Interface

Weapon System

Site Updates

718Hosting.com

Actionscript.org Forums

Flash Game Blogs

Flash Game License

Flashkit Forums

gotoAndLearn

IndieGames.com

Kirupa’s Tutorials

Kongregate Forums

MochiAds Resources

Philip Radvan

Photon Storm

Polygonal

Radvan Media

Tonypa’s Tutorials

Register

Log in

Entries RSS

Comments RSS

WordPress.org