Author Topic: Help with TweenAlpha, not understanding it  (Read 4455 times)

Carwash

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Help with TweenAlpha, not understanding it
« on: June 18, 2014, 04:33:38 PM »
I'm having trouble understanding how to use TweenAlpha repeatedly on the same object.

I have a sprite that is deactivated until 'reset' is clicked, this then activates a sprite that tweens from 0->1 covering the screen with a yellow sprite, and once the tween is finished, it calls another function to reload the scene and remove the yellow sprite.

This works the first time, but any attempt to reset the level again just activates the yellow sprite which remains at an alpha of 1. I've tried a few different things now, and they all end up with the second time having values of from=0 and to=0, OR from=1 and to=1 ...

I can't get my head around setting the to/from values with only 1 attribute in TweenAlpha.Begin()

  1.     float tweenAlphaInt = 1.0f;
  2.     public void ResetLevel()
  3.     {
  4.         NGUITools.SetActive(yellowOverlay, true);
  5.         UITweener resetTween = TweenAlpha.Begin(yellowOverlay, 1.0f, tweenAlphaInt);
  6.     }

The above code works the wrong way first time (tweens from 1->0) and then additional times remain at 0/0.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Help with TweenAlpha, not understanding it
« Reply #1 on: June 18, 2014, 06:02:00 PM »
TweenAlpha.Begin, and in fact all tween.Begins use the current value as the "from" setting. So if the value is currently '0' then it will tween from 0 to 'tweenAlphaInt', which is 1 in your case. If this is not what you want, you can always change the 'from' value:
  1. TweenAlpha tween = TweenAlpha.Begin(yellowOverlay, delay, alphaTo);
  2. tween.from = alphaFrom;

Carwash

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Help with TweenAlpha, not understanding it
« Reply #2 on: June 21, 2014, 10:51:33 AM »
Hey Aren,

Thanks for the help, I've got it mostly working now. However, when I'm going from alpha 0->1 and then the animation is played a second time, the sprite flashes up at an alpha of 1 before doing 0->1.

This video shows what is happening for resetting the level (however the same thing happens on exiting the level) http://screencast.com/t/Jxlh0lg8z Unfortunately, Jing isn't great at screen capture, so you only see the yellow flash on the second reset - even though it does happen on the third and fourth.

Here's my code, how do I get it to reset back to how it was BEFORE it's deactivated, rather than after it's re-activated?

Thanks.

  1.     float tweenAlpha;
  2.         TweenAlpha resetTween = null;
  3.     public void ResetLevel()
  4.     {
  5.         NGUITools.SetActive(yellowOverlay, true);
  6.         tweenAlpha = 1.0f;
  7.         resetTween = TweenAlpha.Begin(yellowOverlay, 1.0f, tweenAlpha);
  8.                 resetTween.from = 0.0f;
  9.     }
  10.  
  11. float greenAlpha;
  12.         TweenAlpha greenTweenLoad = null;
  13.         TweenAlpha greenTweenExit = null;
  14.  
  15.         void GreenLoad()
  16.         {
  17.         NGUITools.SetActive(greenOverlayLoad, true);
  18.         NGUITools.SetActive(greenOverlayExit, false);
  19.         NGUITools.SetActive(yellowOverlay, false);
  20.                 greenAlpha = 0.0f;
  21.                 greenTweenLoad = TweenAlpha.Begin (greenOverlayLoad, 2.0f, greenAlpha);
  22.         greenTweenLoad.from = 1.0f;
  23.         }
  24.  
  25.         public void Fin()
  26.         {
  27.                 NGUITools.SetActive(greenOverlayLoad, false);
  28.         }
  29.  
  30.     void GreenExit()
  31.     {
  32.                 NGUITools.SetActive (greenOverlayExit, true);
  33.                 greenAlpha = 1.0f;
  34.                 greenTweenExit = TweenAlpha.Begin (greenOverlayExit, 1.0f, greenAlpha);
  35.                 greenTweenExit.from = 0.0f;
  36.     }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Help with TweenAlpha, not understanding it
« Reply #3 on: June 21, 2014, 04:01:21 PM »
Set TweenAlpha.value to your target value.

Carwash

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Help with TweenAlpha, not understanding it
« Reply #4 on: June 21, 2014, 05:18:57 PM »
That worked perfectly and I've now got my transitions exactly how I wanted them, thanks very much!