Advertisement
liuwong

button: info

Jun 13th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package example.button
  2. {
  3.     import nape.constraint.LineJoint;
  4.     import nape.phys.Body;
  5.     import nape.phys.Compound;
  6.    
  7.     /**
  8.      * ...
  9.      * @author liu wong
  10.      *
  11.      */
  12.    
  13.     public class buttonInfo
  14.     {
  15.         private static const delta:Number = 0.75;
  16.         private static const delta2:Number = 0.25;
  17.        
  18.         public var compound:Compound;
  19.        
  20.         public var mainBody:Body;
  21.         public var secondBody:Body;
  22.        
  23.         public var len1:Number;
  24.         public var len2:Number;
  25.        
  26.         public var trigger:Boolean = false;
  27.        
  28.         public var counterOn:int = 0;
  29.         public var counterOff:int = 0;
  30.        
  31.         public var onTrigger:Function = null;
  32.        
  33.         public var id:int = 0;
  34.        
  35.         public function buttonInfo(c:Compound, a:Body, b:Body, l:Number):void
  36.         {
  37.             compound = c;
  38.             mainBody = a;
  39.             secondBody = b;
  40.             len1 = l * delta;
  41.             len2 = l * delta2;
  42.         }
  43.        
  44.         public function update():void
  45.         {
  46.             var l:Number = mainBody.position.sub(secondBody.position).length;
  47.            
  48.             if (trigger == false)
  49.             {  
  50.                 if (l <= len2)
  51.                 {
  52.                     trigger = true;
  53.                    
  54.                     counterOn++;
  55.                    
  56.                     if (onTrigger != null)
  57.                         onTrigger(this, true);
  58.                 }
  59.             }
  60.             else
  61.             {  
  62.                 if (l >= len1)
  63.                 {
  64.                     trigger = false;
  65.                    
  66.                     counterOff++;
  67.                    
  68.                     if (onTrigger != null)
  69.                         onTrigger(this, false);
  70.                 }
  71.             }
  72.         }
  73.        
  74.         public function free():void
  75.         {
  76.             compound.space = null;
  77.            
  78.             mainBody = null;
  79.             secondBody = null;
  80.         }
  81.     }
  82.    
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement