Author Topic: Consuming/waiting for new touch  (Read 1815 times)

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Consuming/waiting for new touch
« 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.         }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Consuming/waiting for new touch
« Reply #1 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.