Guest User

Untitled

a guest
Jun 25th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.  
  4.     import flash.display.Sprite;
  5.     import flash.events.Event;
  6.     import flash.events.TimerEvent;
  7.     import flash.utils.Timer;
  8.  
  9.     import nape.geom.Vec2;
  10.     import nape.phys.Body;
  11.     import nape.phys.BodyType;
  12.     import nape.phys.Material;
  13.     import nape.shape.Polygon;
  14.     import nape.space.Broadphase;
  15.     import nape.space.Space;
  16.     import nape.util.ShapeDebug;
  17.  
  18.     [SWF(width=800, height=600, backgroundColor=0x000000, frameRate=30)]
  19.     /**
  20.      *
  21.      */
  22.     public class Nape2KinematicBody extends Sprite
  23.     {
  24.         //
  25.         static public const APP_WIDTH:int = 800;
  26.         static public const APP_HEIGHT:int = 600;
  27.  
  28.         //
  29.         private var _space:Space;
  30.         private var _timeStep:Number = 1/30.0;
  31.         private var _velocityIterations:int = 10;
  32.         private var _positionIterations:int = 10;
  33.  
  34.         private var _debug:ShapeDebug;
  35.         private var _kinematicBody:Body;
  36.         private var _anotherKinematicBody:Body = new Body(BodyType.KINEMATIC);
  37.  
  38.  
  39.         public function Nape2KinematicBody()
  40.         {
  41.             if (stage) init();
  42.             else addEventListener(Event.ADDED_TO_STAGE, init);
  43.         }
  44.  
  45.         /**
  46.          */
  47.         private function init(event:Event = null):void
  48.         {
  49.             removeEventListener(Event.ADDED_TO_STAGE, init);
  50.  
  51.             _space = new Space(new Vec2(0, 400), Broadphase.SWEEP_AND_PRUNE);
  52.             createBodies();
  53.             initDebugDraw();
  54.  
  55.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  56.         }
  57.  
  58.         /**
  59.          */
  60.         private function createBodies():void
  61.         {
  62.             _kinematicBody = new Body(BodyType.KINEMATIC);
  63.             _kinematicBody.velocity.setxy(0, 200);
  64.  
  65.             var kinematicShape:Polygon = new Polygon(Polygon.box(300, 50), new Material());
  66.             _kinematicBody.shapes.add(kinematicShape);
  67.  
  68.             var dynamicBoxShape:Polygon = new Polygon(Polygon.box(50, 50), new Material());
  69.             var dynamicBody:Body = new Body(BodyType.DYNAMIC);
  70.             dynamicBody.shapes.add(dynamicBoxShape);
  71.  
  72.             _anotherKinematicBody.shapes.add(new Polygon(Polygon.box(600, 50), new Material()));
  73.  
  74.             //
  75.             _kinematicBody.position.setxy(APP_WIDTH/2, 300);
  76.             _anotherKinematicBody.position.setxy(APP_WIDTH/2, 300);
  77.             dynamicBody.position.setxy(APP_WIDTH/2, 250);
  78.  
  79.             //
  80.             _kinematicBody.space = _space;
  81.             _anotherKinematicBody.space = _space;
  82.             dynamicBody.space = _space;
  83.  
  84.             //
  85.             var timer:Timer = new Timer(6500, 0);
  86.             timer.addEventListener(TimerEvent.TIMER, moveAnotherPlatform);
  87.             timer.start();
  88.         }
  89.  
  90.         private var _isMove:Boolean = false;
  91.  
  92.         /**
  93.          */
  94.         private function moveAnotherPlatform(event:TimerEvent):void
  95.         {
  96.             _isMove = !_isMove;
  97.  
  98.             if (_isMove) _anotherKinematicBody.velocity.setxy(0, 20);
  99.             else _anotherKinematicBody.velocity.setxy(0, 0);
  100.         }
  101.  
  102.         /**
  103.          */
  104.         private function initDebugDraw():void
  105.         {
  106.             _debug = new ShapeDebug(APP_WIDTH, APP_HEIGHT, 0x000000);
  107.             addChild(_debug.display);
  108.         }
  109.  
  110.  
  111.         /**
  112.          */
  113.         private function enterFrameHandler(event:Event):void
  114.         {
  115.             _space.step(_timeStep, _velocityIterations, _positionIterations);
  116.  
  117.             _debug.clear();
  118.             _debug.draw(_space);
  119.             _debug.flush();
  120.  
  121.             updateKinematicPosition();
  122.         }
  123.  
  124.         /**
  125.          */
  126.         private function updateKinematicPosition():void
  127.         {
  128.             if (_kinematicBody.position.y > 600)
  129.             {
  130.                 _kinematicBody.velocity.y = -_kinematicBody.velocity.y;
  131.                 _anotherKinematicBody.velocity.y = -_anotherKinematicBody.velocity.y;
  132.             }
  133.             else if (_kinematicBody.position.y < 50)
  134.             {
  135.                 _kinematicBody.velocity.y = -_kinematicBody.velocity.y;
  136.                 _anotherKinematicBody.velocity.y = -_anotherKinematicBody.velocity.y;
  137.             }
  138.         }
  139.     }
  140. }
Add Comment
Please, Sign In to add comment