Author Topic: TweenPosition and OnFinished sending same event?  (Read 4206 times)

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
TweenPosition and OnFinished sending same event?
« on: January 11, 2014, 01:38:58 PM »
Hey there,

so I spent the last hours trying to get to know the scripting side of NGUI (working with the latest version), especially the new way handling Events. I looked up the Upgrade-Tutorials and used the Searchfunction and got things to work.
Now I have a little problem. I don't know if it's my own stupidity or maybe a bug, so please enlighten me :D

I want to achieve this:

I have a Button at the Bottom of the Screen and a Sprite which is positioned by default offscreen at the top of the screen.
When the Button is hit the first time, the Sprite should come down (using TweenPosition) and stay at the middle of the screen.
Then, when the Button is hit again that certain sprites goes back to its initial position offscreen.

This is what I'm doing right now:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class BookButton : MonoBehaviour
  5. {
  6.  
  7.                 public bool firstTap;
  8.                 private GameObject gameBook;
  9.                 private UIButton btn;
  10.  
  11.                 // Use this for initialization
  12.                 void Start ()
  13.                 {
  14.  
  15.                                 firstTap = false;
  16.                                 gameBook = GameObject.FindGameObjectWithTag ("GAMEBOOK");
  17.                                 btn = GetComponent<UIButton> ();
  18.        
  19.                 }
  20.  
  21.                 public void BookClick ()
  22.                 {
  23.  
  24.                                 if(firstTap)
  25.                                 {
  26.                                 TweenPosition tp = TweenPosition.Begin(gameBook, 1.0f, new Vector3(gameBook.transform.localPosition.x,
  27.                                                                                              350.0f,
  28.                                                                                              gameBook.transform.localPosition.z));
  29.                                 EventDelegate.Add(tp.onFinished, Fin1);
  30.                                 }
  31.  
  32.                         else
  33.                         {
  34.                         TweenPosition tp2 = TweenPosition.Begin(gameBook, 1.0f, new Vector3(gameBook.transform.localPosition.x,
  35.                                                                                            -350.0f,
  36.                                                                                            gameBook.transform.localPosition.z));
  37.                         EventDelegate.Add(tp2.onFinished, Fin2);
  38.                         }
  39.  
  40.  
  41.  
  42.                 }
  43.  
  44.  
  45.         void Fin1()
  46.         {
  47.                 firstTap = false;
  48.                 Debug.Log("FIN1");
  49.         }
  50.  
  51.         void Fin2()
  52.         {
  53.                 firstTap = true;
  54.                 Debug.Log("FIN2");
  55.  
  56.         }
  57. }
  58.  

When I hit PLAY in Unity and start clicking the Button the first time the Sprite comes down, when I hit it again it goes back up, hitting it again it comes back down. Hitting it again, nothing happens. It doesn't move back up.
What is interesting though is the change of my firstTap-bool:

First Time hitting the Button: sprites comes down -> true
Second Time hitting the Button: sprites comes down -> false
Third Time hitting the Button: sprites comes down -> false

Another more interesting thing is:
When I hit the Button the first time, Debug.Log prints "FIN2", which is right, but when I hit it again in order to move the sprite back Debug.Log prints "FIN1" but also "FIN2" again! That shouldn't be the case, because TweenPosition tp is played and finished, why is the EventDelegate sending both Events? Did I miss something?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TweenPosition and OnFinished sending same event?
« Reply #1 on: January 11, 2014, 05:41:16 PM »
EventDelegate.Add has an optional 3rd parameter that determines whether the delegate will be removed after it gets executed once.

You need to pass 'false' for it to work like you are trying it to make it work, or the delegate will stick around.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: TweenPosition and OnFinished sending same event?
« Reply #2 on: January 11, 2014, 06:10:54 PM »
You mean like this:
  1. EventDelegate.Add(tp.onFinished, Fin1, false);
  2.  

Hm, if I pass "false" as third argument, nothing changes. But if I use "true" it works as it should and in the Inspector the particular OnFinished-Notification shows up and is removed after finishing.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TweenPosition and OnFinished sending same event?
« Reply #3 on: January 11, 2014, 06:12:49 PM »
Right, I got them backwards then, sorry. I'm still on vacation here without access to my work computer.

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: TweenPosition and OnFinished sending same event?
« Reply #4 on: January 11, 2014, 06:32:17 PM »
Yeah, but I thought NOT removing was the point of it.
I thought by ADDING the notification STAYS there and will be notified everytime the tween is finished, but obviously it doesn't do that, at least not the way I want to...

This way it is removed and "created" anew. It seems to work and suits my needs, but I just thought the other way would be more logical to work with.