Advertisement
liuwong

nape example: button

Jun 13th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package example
  2. {
  3.     import example.button.buttonInfo;
  4.     import example.button.buttonUtils;
  5.     import flash.display.DisplayObjectContainer;
  6.     import nape.geom.Vec2;
  7.     import nape.phys.Body;
  8.    
  9.     /**
  10.      * ...
  11.      * @author liu wong
  12.      *
  13.      */
  14.    
  15.     public class buttonExample extends baseExample
  16.     {
  17.         private const spawnPos:Vec2 = new Vec2(50, 50);
  18.        
  19.         private var buttons:Array/*buttonInfo*/;
  20.        
  21.         private var circle:Body;
  22.        
  23.         public function buttonExample(prnt:DisplayObjectContainer):void
  24.         {
  25.             super(prnt);
  26.         }
  27.        
  28.         override protected function init():void
  29.         {
  30.             super.init();
  31.            
  32.             space.gravity.setxy(0, 980);
  33.            
  34.             addHand();
  35.            
  36.             addWalls();
  37.            
  38.             var k:int = 12;
  39.             var i:int;
  40.            
  41.             addRectangle(320, 300, 580, 10, (Math.PI / 180) * 10);
  42.            
  43.             buttons = new Array();
  44.            
  45.             for (i = 0; i < k; i++)
  46.             {
  47.                 var button:buttonInfo = buttonUtils.createButton(space, new Vec2((640 / (k + 1)) * (i + 1), 200+(100 / (k + 1)) * (i + 1)), 45, 20, 35, (Math.PI / 180) * 10);
  48.                 button.id = i;
  49.                 button.onTrigger = onButton;
  50.                 buttons.push(button);
  51.             }
  52.            
  53.             circle = addCircle(spawnPos.x, spawnPos.y, 25, 0, false);
  54.            
  55.             var b:Body = addRectangle(320, 460, 640, 10);
  56.             b.surfaceVel.setxy( -100, 0);
  57.         }
  58.        
  59.         private function onButton(src:buttonInfo, value:Boolean):void
  60.         {
  61.             if (value == true)
  62.             {
  63.                 if (src.id == 11)
  64.                 {
  65.                     circle.position.set(spawnPos);
  66.                     return;
  67.                 }
  68.                
  69.                 if (src.id == 7)
  70.                 {
  71.                     addRectangle(620, 40, 10, 10, Math.random() * Math.PI * 2, false);
  72.                     return;
  73.                 }
  74.             }
  75.         }
  76.        
  77.         override public function update():void
  78.         {
  79.             super.update();
  80.            
  81.             var k:int = buttons.length;
  82.             var i:int;
  83.            
  84.             for (i = 0; i < k; i++)
  85.                 buttons[i].update();
  86.         }
  87.     }
  88.    
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement