- Home /
 
 
               Question by 
               alcohollica · Aug 03, 2014 at 02:25 PM · 
                javascriptguibuttonngui  
              
 
              NGUI Repeat button In JavaScript problem
I'm work in JavaScript I make movement button in NGUI so I need repeat button according to my search I found this,
 public class RepeatButtons : MonoBehaviour
 {
     public float delay = 1.00f;
     public float interval = 0.25f;
 
     bool mIsPressed = false;
     float mNextClick = 0f;
 
     void OnPress (bool isPressed) 
     { 
         mIsPressed = isPressed;
         mNextClick = Time.realtimeSinceStartup + delay; 
 
         // Perform action as soon as button is pressed.
         if (isPressed)
             PerformAction();
     }
     
     void Update ()
     {
         // Adjusted condition slightly...
         if (mIsPressed && Time.realtimeSinceStartup >= mNextClick)
         {
             mNextClick = Time.realtimeSinceStartup + interval;
             PerformAction();
         }
     }
 
     void PerformAction() {
         //Do something
     }
 }
 
               I try to something but it is not working.
 public var delay    : float  = 1.00f;
 public var interval : float  = 0.25f;
 
 var mIsPressed : boolean = false;
 var mNextClick : float   = 0f;
 var isPressed : boolean = true;
 
 function OnPress () 
 { 
     mIsPressed = isPressed;
     mNextClick = Time.realtimeSinceStartup + delay; 
 
      // Perform action as soon as button is pressed.
     if(isPressed){
         PerformAction();
     }
 }
 
 
     
 function Update ()
 {
         // Adjusted condition slightly...
     if(mIsPressed && Time.realtimeSinceStartup >= mNextClick)
     {
         mNextClick = Time.realtimeSinceStartup + interval;
         PerformAction();
     }
 }
 
 function PerformAction() {
     Debug.Log("Hey ho");
 }
 
               How I convert this to JS?
               Comment
              
 
               
              Answer by Saad_Khawaja · Aug 03, 2014 at 02:57 PM
You are not taking the bool parameter in OnPress:
This should do the trick in JS.
 #pragma strict
     var delay: float  = 1.00f;
     var interval: float  = 0.25f;
     
     var mIsPressed:boolean = false;
     var mNextClick: float  = 0.25f;
     
     function OnPress (isPressed:boolean) 
     { 
         mIsPressed = isPressed;
         mNextClick = Time.realtimeSinceStartup + delay; 
         
         // Perform action as soon as button is pressed.
         if (isPressed)
             PerformAction();
     }
     
     function Update ()
     {
         // Adjusted condition slightly...
         if (mIsPressed && Time.realtimeSinceStartup >= mNextClick)
         {
             mNextClick = Time.realtimeSinceStartup + interval;
             PerformAction();
         }
     }
     
     function PerformAction() {
         Debug.Log("late button");
         //Do something
     }
 
 
              Thanks but I get NullReferenceException: Object reference not set to an instance of an object error
 var speed : float = 50.0f;
 var tar : Transform;
 var delay: float  = 1.00f;
 var interval: float  = 0.25f;
  
 var mIsPressed:boolean = false;
 var mNextClick: float  = 0.25f;
  
 function OnPress (isPressed:boolean) 
 { 
     mIsPressed = isPressed;
     mNextClick = Time.realtimeSinceStartup + delay; 
  
     // Perform action as soon as button is pressed.
     if (isPressed)
         PerformAction();
 }
  
 function Update ()
 {
         // Adjusted condition slightly...
     if (mIsPressed && Time.realtimeSinceStartup >= mNextClick)
     {
         mNextClick = Time.realtimeSinceStartup + interval;
         PerformAction();
     }
 }
  
 function PerformAction() {
     
     tar.transform.x -= speed * Time.deltaTime;
 }
                 Your answer