# Strategy for controlling the physics of a bike

I'm new(ish) to Actionscript 3 and I was wondering what the code what would be like for controlling the bike in a game.

How would the animation of the bike work? Would I have to create a bike animation of the wheels moving and make it start and stop when the key to go forward is pressed. How would I go about it slowly stopping when the key to go forward is released?

-
IT is hard to say what physics engine would be best for you without knowing more about the game. Is it heavily physics based then yes, Box2D is problably a good alternative for you. If it is not and you just need something to mova a bike around then you might be better writing the physics or game logic you need yourself. – Mikael Högström Aug 1 '12 at 14:31
You're asking two different questions at once - please don't do that! Your asking whether Box2D is "the best" is a "what technology should I use?" question which is off topic as defined in the FAQ, so you should just stick with asking about how to make the bike work. – doppelgreener Aug 21 '12 at 2:46

I think you will learn a lot using Box2d, and even if at the end you prefer to write the code yourself for a simple collision effect, you won't lost what you would have learned.

About your second question, i guess it is like a jump with the gravity : in a function called every frame (onEnterFrame), you can decrement a counter for the rotation, and slowly get closer to 0 rotation, when you release the button. With Box2d, you can do this with a "motor" i think, look at this link for more informations :

motor with box2d

-

To make the bike not only go forward, but also slow down, you need to apply decay when key is released.

Pseudocode:

var speed = 0;
var decay = 0.97;
var keyIsPressed = false;

// on key press:
keyIsPressed = true;

// on key release:
keyIsPressed = false;

// on enter frame:
if keyIsPressed:
speed += 1;
else
speed *= decay;

On every frame, if the accelerate key is held down, then the decay will slow it down and speed value will return to 0 over time.

-