In this flash actionscript 3.0 tutorial you will see how to make a car move more realistic then just moving back and forth, we will add some acceleration and de acceleration functionality so the car start up slow and increases in speed until it ends at a max speed, then when we want to stop it also need to slow down before it stops. And it all should be looking like this. First you may prepare some graphics for you stage, I guess you can do better then I did, I just made a fast one here. One simple tip I will tell you is to add a drop shadow to your object, so it looks like its standing on some kind of ground. Now the only thing we need on the stage is some kind of car, just draw something or put in one if you have one. Convert it to a movie clip and give it the instance name "car". Now we are ready for the actionscript part, so open up your actionscript panel and type in the code below. First we need some variables to control some stuff, like telling us which key is pressed, the speed of the car, friction and acc.. Usually I always put an init function, we could just type it outside any function, but it all looks more structured with a init function for all the code that should be run at program start. First the function makes sure to tell flash that no keys is down. Then we add to eventlisteners, one for the key down, and one for key up, to call a function no matter what key is hit. This next function "filters" what key is down, if its the left or right, then add some function if its the one or other. And here is the function that has the most important code, it actually makes the car move, when the right key is down. As you may have noticed in the startMe function we also added an eventlistner to listen to when the key is UP again, that is no key is down, so we can stop the car again. And that''s it, if you had any problem with the code, you may download the source code below, and test the car function through there.
Car Acceleration Tutorial
Label: tutorial flash
Subscribe to:
Post Comments (Atom)
0 komentar:
Post a Comment