Realistic Car Movement

July 19, 2010

Realistic Car Movement

In this tutorial you will learn how to make a cars wheels spin when it moves, and for it to speed up and slow down as it moves. I am using flash 8, but Flash MX 2004 will work as well.

You can see an example of what we will be making HERE.

First of all you will need to draw your cars wheel. Once you have done that select it and make it a MC (Movie Clip). Make sure the registration point is in the center.

Making the wheel

Now, copy and paste the wheel so your car will have two wheels. Give one of them an instance name of ‘wheel1′ and the other an instance name of ‘wheel2′. Now draw the body of the car and make the whole thing a movieclip. Give this MC an instance name of ‘car’.

Making the car

So now you should have your car MC, which contains your two wheel MCs, wheel1 and wheel2, as well as the rest of the car. Now paste this code onto the first frame.

var speed:Number = 0;
var slow:Number = 0.90;
var maxSpeed:Number = -10;
onEnterFrame = function () {
    if (Key.isDown(Key.RIGHT)) {
        speed += 1;
    } else if (Key.isDown(Key.LEFT)) {
        speed -= 1;
    } else {
        speed *= slow;
    }
    if ((speed<0.5 && speed>0) || (speed>-0.5 && speed<0)) {
        speed = 0;
    }
    _root.car._x += speed;
    for (i=1; i<3; i++) {
        _root.car["wheel"+i]._rotation += speed;
    }
    if(speed < maxSpeed){
        speed = maxSpeed;
    }
    if(speed > Math.abs(maxSpeed)){
        speed = Math.abs(maxSpeed);
    }
};

Now to explain the code.

Lines 1-3 are declaring some variables. The first one, speed, well tell the car how fast to move and tell the wheels how fast to spin. The second one, slow, is the rate the car will slow down at when no buttons are pressed. If it is 1, the car will not slow down. If it is above one the car will speed up instead of slowing down. If is it 0 the car will slow down immediately. So the faster you want your car to slow down, the lower you have the number.
The third variable declared is the maximum speed the car can move. It is set to 10 pixels a frame at the moment. You might be wondering why it is a negative number but I will explain that later.

Line 4 as always means ‘Every time this frame is entered do the following’. So if your frame rate is 12, the code will run 12 times a second.

Lines 5-11 are what makes the car speed up and slow down. If the right arrow is pressed down the speed of the car will increase by 1. If the left arrow key is pressed down the speed of the car will decrease by 1. If the speed is in the negatives (eg -4) the car will move backwards. If none of the buttons are pressed the speed variable is being multiplied by the slow variable we declared so it loses speed.

Lines 12-14 is telling flash that is the car is moving slower than 0.5 pixels than to stop it moving. Otherwise the car will never stop, and will end up moving a speed like 0.0000001.

Line 15 is what makes the car move. It tells flash to make the car move as fast as the variable speed. So if speed is equal to 3, the car will move 3 pixels.

Lines 16-18 are making the wheels spin. There is a for statement there, so you can just write wheel+i instead of wheel1 and wheel2.

Lines 19-21 are stopping the car from moving a speed more than the max speed backwards, which is -10.

Lines 22-24 are stopping the car from moving a speed more than the max speed forwards. Since the max speed is -10, that wouldn’t work so I added in a little maths function. Math.abs() converts a negative number to a positive number.

now just press ctrl + enter and have fun!!!


source

 

 

Buttons in Flash using ActionScript 3.0: Going to next frame and previous frame

July 9, 2010

Buttons in Flash using ActionScript 3.0: Going to next frame and previous frame


Some people have been asking how to create button code in Flash (CS3, CS4, or CS5) that progresses the playhead in a SWF to the next or previous frame using ActionScript 3.0. This is not too difficult, and is a super simple code modification from any other button in AS3 (and is actually a touch easier than AS2 due to scoping issues).

This post includes an example of how to create two buttons that click between a few...


Continue reading...
 

How to Create iPhonw Apps in Flash cs5: (Windows And Mac)

July 9, 2010
With the release of Adobe's Flash CS5 comes the ability to compile iPhone apps inside of Flash and then export them into .IPA files with the ability to distribute them into the App Store or into Cydia. Weather you don't want to pay Apple's $100 registration fee or you don't have a Mac, this guide will show how you can create your own iPhone app and be able to distribute them on to other devices.

1. Download Adobe Flash Professional CS5. You can either buy it for $699 or get the free 30 day tri...

Continue reading...
 

Mac or Pc

July 6, 2010
So for the first post i want to address the mac vs. pc argument that has been going around basically since both were created. I used to share a desktop PC  for a couple years feeling it getting slower and slower day by day.But then one day i was given a mac, mind you not it was not a new one. I received it in 2005 and it was a 2001 macbook. but this machine was ok at first but then sites were updated and it just couldn't handle it. Finally at the final months of Dell 1545 laptop. don't get my...
Continue reading...
 

Blog Archive

 
Make a Free Website with Yola.