Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Zophiel on April 04, 2013, 07:02:04 PM

Title: Consuming/waiting for new touch
Post by: Zophiel on April 04, 2013, 07:02:04 PM
Hello,

I've written a spell casting system. Pressing a button will set a spell's state to ready. The spell then waits for a touch phase ended to trigger. But pressing a NGUI button also sends that event to the spell casting it when you let go of the NGUi button.

What's the best way to handle something like this?

  1. void OnPress( bool isPressed )
  2.         {
  3.                 if(!isPressed) EquipmentManager.Instance.UseRune( runeIndex );
  4.         }

  1. override public void Use()
  2.         {
  3.                 if( isReadyToCast ) base.OnEffectCancel();
  4.                 else isReadyToCast = true;
  5.         }
  6.  
  7. override public void Update()
  8.         {
  9.                 if( isReadyToCast )
  10.                 {
  11.                         Debug.Log( "Ready!" );
  12.  
  13.                         if( Input.touchCount > 0 && Input.touches[ 0 ].phase == TouchPhase.Ended )
  14.                                 Debug.Log( "Cast Spell!" );
  15.                 }
  16.         }
Title: Re: Consuming/waiting for new touch
Post by: ArenMook on April 05, 2013, 12:04:53 PM
You're approaching it from the wrong end. Script to cast the spell should be on the button. If you want a specific keyboard key to trigger the spell as well, just add a UIButtonKeyBinding to that button. You don't need to write any code to trigger anything this way. Just your OnClick() function.