package { import org.cove.ape.*; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; /** * This is an example showing use of the WheelParticles and collidable * SpringConstraints used as surfaces in the 'bridge' area. The bouncy * area has a high elasticity setting to push the car back up to the top. */ [SWF(width="650", height="350", backgroundColor="#334433")] public class physic_flex_ape_sample_car_demo extends Sprite { private static var colA:uint = 0x334433; private static var colB:uint = 0x3366aa; private static var colC:uint = 0xaabbbb; private static var colD:uint = 0x6699aa; private static var colE:uint = 0x778877; private var car:physic_flex_ape_sample_car_Car; private var rotator:physic_flex_ape_sample_car_Rotator; public function physic_flex_ape_sample_car_demo() { // set up the events, main loop handler, and the engine. you don't have to use // enterframe. you just need to call the ApeEngine.step() and ApeEngine.paint() // wherever and however you're handling your program cycle. stage.frameRate = 55; stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); addEventListener(Event.ENTER_FRAME, run); // Initialize the engine. The argument here is the time step value. // Higher values scale the forces in the sim, making it appear to run // faster or slower. Lower values result in more accurate simulations. APEngine.init(1/4); // set up the default diplay container APEngine.container = this; // gravity -- particles of varying masses are affected the same APEngine.addMasslessForce(new AVector(0, 3)); // groups - all these classes extend group var surfaces:physic_flex_ape_sample_car_Surfaces = new physic_flex_ape_sample_car_Surfaces(colA, colB, colC, colD, colE); APEngine.addGroup(surfaces); var bridge:physic_flex_ape_sample_car_Bridge = new physic_flex_ape_sample_car_Bridge(colB, colC, colD); APEngine.addGroup(bridge); var capsule:physic_flex_ape_sample_car_Capsule = new physic_flex_ape_sample_car_Capsule(colC); APEngine.addGroup(capsule); rotator = new physic_flex_ape_sample_car_Rotator(colB, colE); APEngine.addGroup(rotator); var swingDoor:physic_flex_ape_sample_car_SwingDoor = new physic_flex_ape_sample_car_SwingDoor(colC); APEngine.addGroup(swingDoor); car = new physic_flex_ape_sample_car_Car(colC, colE); APEngine.addGroup(car); // determine what collides with what. car.addCollidableList(new Array(surfaces, bridge, rotator, swingDoor, capsule)); capsule.addCollidableList(new Array(surfaces, bridge, rotator, swingDoor)); } private function run(evt:Event):void { APEngine.step(); APEngine.paint(); rotator.rotateByRadian(.02); } private function keyDownHandler(keyEvt:KeyboardEvent):void { var keySpeed:Number = 0.2; if (keyEvt.keyCode == 65) { car.speed = -keySpeed; } else if (keyEvt.keyCode == 68) { car.speed = keySpeed; } } private function keyUpHandler(keyEvt:KeyboardEvent):void { car.speed = 0; } } }