Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.TimerEvent;
- import flash.utils.Timer;
- import nape.geom.Vec2;
- import nape.phys.Body;
- import nape.phys.BodyType;
- import nape.phys.Material;
- import nape.shape.Polygon;
- import nape.space.Broadphase;
- import nape.space.Space;
- import nape.util.ShapeDebug;
- [SWF(width=800, height=600, backgroundColor=0x000000, frameRate=30)]
- /**
- *
- */
- public class Nape2KinematicBody extends Sprite
- {
- //
- static public const APP_WIDTH:int = 800;
- static public const APP_HEIGHT:int = 600;
- //
- private var _space:Space;
- private var _timeStep:Number = 1/30.0;
- private var _velocityIterations:int = 10;
- private var _positionIterations:int = 10;
- private var _debug:ShapeDebug;
- private var _kinematicBody:Body;
- private var _anotherKinematicBody:Body = new Body(BodyType.KINEMATIC);
- public function Nape2KinematicBody()
- {
- if (stage) init();
- else addEventListener(Event.ADDED_TO_STAGE, init);
- }
- /**
- */
- private function init(event:Event = null):void
- {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- _space = new Space(new Vec2(0, 400), Broadphase.SWEEP_AND_PRUNE);
- createBodies();
- initDebugDraw();
- addEventListener(Event.ENTER_FRAME, enterFrameHandler);
- }
- /**
- */
- private function createBodies():void
- {
- _kinematicBody = new Body(BodyType.KINEMATIC);
- _kinematicBody.velocity.setxy(0, 200);
- var kinematicShape:Polygon = new Polygon(Polygon.box(300, 50), new Material());
- _kinematicBody.shapes.add(kinematicShape);
- var dynamicBoxShape:Polygon = new Polygon(Polygon.box(50, 50), new Material());
- var dynamicBody:Body = new Body(BodyType.DYNAMIC);
- dynamicBody.shapes.add(dynamicBoxShape);
- _anotherKinematicBody.shapes.add(new Polygon(Polygon.box(600, 50), new Material()));
- //
- _kinematicBody.position.setxy(APP_WIDTH/2, 300);
- _anotherKinematicBody.position.setxy(APP_WIDTH/2, 300);
- dynamicBody.position.setxy(APP_WIDTH/2, 250);
- //
- _kinematicBody.space = _space;
- _anotherKinematicBody.space = _space;
- dynamicBody.space = _space;
- //
- var timer:Timer = new Timer(6500, 0);
- timer.addEventListener(TimerEvent.TIMER, moveAnotherPlatform);
- timer.start();
- }
- private var _isMove:Boolean = false;
- /**
- */
- private function moveAnotherPlatform(event:TimerEvent):void
- {
- _isMove = !_isMove;
- if (_isMove) _anotherKinematicBody.velocity.setxy(0, 20);
- else _anotherKinematicBody.velocity.setxy(0, 0);
- }
- /**
- */
- private function initDebugDraw():void
- {
- _debug = new ShapeDebug(APP_WIDTH, APP_HEIGHT, 0x000000);
- addChild(_debug.display);
- }
- /**
- */
- private function enterFrameHandler(event:Event):void
- {
- _space.step(_timeStep, _velocityIterations, _positionIterations);
- _debug.clear();
- _debug.draw(_space);
- _debug.flush();
- updateKinematicPosition();
- }
- /**
- */
- private function updateKinematicPosition():void
- {
- if (_kinematicBody.position.y > 600)
- {
- _kinematicBody.velocity.y = -_kinematicBody.velocity.y;
- _anotherKinematicBody.velocity.y = -_anotherKinematicBody.velocity.y;
- }
- else if (_kinematicBody.position.y < 50)
- {
- _kinematicBody.velocity.y = -_kinematicBody.velocity.y;
- _anotherKinematicBody.velocity.y = -_anotherKinematicBody.velocity.y;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment