Guest User

Untitled

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