Free ActionScript

Flash AS2 & AS3 Tutorials, Game Code, Effects, Source Files & Sample Downloads

Car Movement: Acceleration, Braking, Turning & Reverse

17 Comments »

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

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/

17 Responses

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

  • 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!

  • 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

    • 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.

    :grin:

    -PR

  • [...] 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 :grin:

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

  • Leave a Reply

    You must be logged in to post a comment.