Author Topic: Issue setting variable with OnPress()  (Read 5300 times)

HypoXic5665

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Issue setting variable with OnPress()
« on: November 18, 2012, 01:31:53 AM »
Hi, I am having an issue with an OnPress() for my mobile game. I am sure it is just from my lack of understanding of how the touch functions work with NGUI. If anyone could help clarify or point out what my mistake might be it would be appreciated.

So pretty much I have two scripts, Button.js and ControlCenter.js. When the NGUI button is pressed I am setting the boolean <nguiTouched = true> (via Button.js)  which lives in the ControlCenter.js. This is working how I would expect except for when the button is released the part of the ControlCenter Update function, marked with **, is getting processed and saving the location of the touch release.

The block of code marked should not be getting read since it is inside of the (!nguiTouched) conditional unless I am not understanding how the OnPress() function is called. Am I not using this function properly or is there another function I should be using here?

Thanks in advance,

Button.js
  1. var controlCenter: ControlCenter;
  2.  
  3. private var pressed: boolean = false; //is this button bieng pressed?
  4.  
  5. function OnPress()
  6. {
  7.         if(!pressed)
  8.         {
  9.                 pressed = true;
  10.                 controlCenter.nguiTouched = true;
  11.         }
  12.         else
  13.         {
  14.                 pressed = false;
  15.                 controlCenter.nguiTouched = false;
  16.         }
  17. }

ControlCenter.js
  1.  
  2. var nguiTouched: boolean = false;
  3.  
  4. function Update ()
  5. {
  6.         if(!nguiTouched)
  7.         {
  8.                 if(Input.touchCount > 0) //if the user presses the screen...
  9.                 {
  10.                         var touch: Touch = Input.touches[0];
  11.                         touchTime += Time.deltaTime; //to avoid accidental presses
  12.                                                                        
  13.                         **if(needInitialTouchLoc) //get the loc of the initial touch position **
  14.                         **{                                                                                                  **
  15.                         **      initialTouchLoc = Input.GetTouch(0).position;                         **
  16.                         **      needInitialTouchLoc = false;                                                    **
  17.                         **      print("initialTouchLoc: " + initialTouchLoc);                              **
  18.                         **}                                                                                                  **
  19.  
  20.            if(other parameters)
  21.           {
  22.                      do other stuff....
  23.            }
  24.      }
  25.   }
  26. }
  27.  
  28.  
  29.  

AndyGFX

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 24
    • View Profile
    • AndyGFX
Re: Issue setting variable with OnPress()
« Reply #1 on: November 18, 2012, 03:29:20 AM »
OnPress function return press/release state via argument, when script is assigned on UIButton component.
true - when button is pressed
false - when button is up

void OnPress(bool isPressed) ....

http://www.tasharen.com/forum/index.php?topic=2249.msg11218#msg11218
« Last Edit: November 18, 2012, 05:44:18 AM by AndyGFX »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Issue setting variable with OnPress()
« Reply #2 on: November 18, 2012, 06:48:47 AM »
You are doing double input handling which is a bit weird. Don't do it with both OnPress and inside an Update, that will only lead to headaches.

If you change your OnPress to have a bool parameter, this will be the pressedStatus (true = pressed, false = released) then you can set whatever things you want based on that. Then you can also remove all the input.yaddyadda from the update function.

HypoXic5665

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Issue setting variable with OnPress()
« Reply #3 on: November 18, 2012, 02:18:41 PM »
Thanks Nicki! I definitely was using the OnPress() entirely wrong and being redundant. I'm not sure if my code is as efficient as possible now but i'm definitely going the right way now.

Thanks again.