Controlling motors with keyboard

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.

  1. 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.*;
  2. 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 ...

Get Box2D for Flash Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.