We want the player to control the truck with arrow keys. Left arrow key will move the truck to the left and right arrow key will move the truck to the right.
To let the player control motors with keyboard, you first need to include the
KeyboardEvent
class, which will allow us to create keyboard listeners:import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Joints.*;
Then, we need some new class-level variables:
private var world:b2World; private var worldScale:Number=30; private var left:Boolean=false; private var right:Boolean=false; private var frj:b2RevoluteJoint; private var rrj:b2RevoluteJoint; private var motorSpeed:Number=0;
left
andright
are Boolean variables, which will let us know if left or right arrow keys are being pressed.frj
andrrj
are the front and rear revolute joint...