Author Topic: Only one tweenPosition plays at a time  (Read 7743 times)

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
Only one tweenPosition plays at a time
« on: February 14, 2014, 05:30:22 PM »
And I've run into yet another problem trying to implement tweens.
I've got a "ticker" on the bottom, with headlines form left to right. what's supposed to happen is it adds a new item off screen to the left, then tweens all the headlines to the right based on the new items width (putting the new item in the bottom left corner).

This only works for the FIRST item, though, as every other item just jumps to the new position.

On a related note, is there a way to force the anchor resizing? the headline background/button is supposed to be the width of the label which works, but when I'm adding the item and tweening everything, the headline background width hasn't resized according to its anchors yet, so the width is wrong (uses the default prefab size). I got it to work by looking at the label's width and manually adding the sum of spacing around it, but that's not ideal.
(int width = 40 + 6 + uipLine.Label.width + 10;//icon+spacing+label+spacing)
  1. void AddActiveHeadline(Headline headline)
  2.         {
  3.                 Debug.Log("Adding Headline:" + headline.Title);
  4.                 //create GUI element
  5.                 UIPHeadlineButton uipLine = UIPHeadlineButton.Create(this.TickerBar, headline);
  6.                 //get width for positioning, tweens
  7.                 int width = uipLine.Background.width;//TODO returns INCORRECT WIDTH, not yet resized according to anchors
  8.                 //move new headline to bottomleft, off screen by width
  9.                 Vector3 offPos = uipLine.transform.localPosition;
  10.                 offPos.x -= width;
  11.                 uipLine.transform.localPosition = offPos;//move offscreen by width
  12.                 //add to active headlines
  13.                 this.ActiveHeadlines.Add(uipLine);             
  14.                 //slide headlines
  15.                 Debug.Log("Tweening Headlines");
  16.                 foreach (UIPHeadlineButton hdln in ActiveHeadlines)
  17.                 {
  18.                         hdln.TweenPos.SetStartToCurrentValue();
  19.                         //get "to" pos
  20.                         Vector3 pos = hdln.transform.localPosition;
  21.                         pos.x += width;
  22.                         hdln.TweenPos.to = pos;
  23.                         //play
  24.                         hdln.TweenPos.PlayForward();
  25.                 }
  26.         }
  27.  

EDIT: I've tried this with a for loop instead in case that was the problem, but same issue: only the first tween executes, and the other items just "jump" to the "to" position.
I also noticed a strange thing... If I click on the object in the inspector and activate the tween component, it stays active for as long as the tween *would* take to play, then jump to the end position and turns off. so it's acting like it's playing, but it's not actually animating out the tween.

  1. for (int i = ActiveHeadlines.Count-1; i>=0; i--)
  2.                 {
  3.                         UIPHeadlineButton hdln = ActiveHeadlines [i];
  4.                         hdln.TweenPos.SetStartToCurrentValue();
  5.                         //get "to" pos
  6.                         Vector3 pos = hdln.transform.localPosition;
  7.                         pos.x += width;
  8.                         hdln.TweenPos.to = pos;
  9.                         //play
  10.                         hdln.TweenPos.PlayForward();
  11.                 }
  12.  
« Last Edit: February 14, 2014, 08:46:35 PM by sintua »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Only one tweenPosition plays at a time
« Reply #1 on: February 15, 2014, 01:46:34 AM »
Don't attach tweens if you're just going to activate them through code.

Use TweenPosition.Begin() to create the tweens and initialize them properly instead of setting 'to' and calling PlayForward.

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
Re: Only one tweenPosition plays at a time
« Reply #2 on: February 15, 2014, 02:32:14 PM »
I switched to begin and it... sort of works? Begin always overwrites everything that's not in the parameters (overwrites animation curve, for example), so I switched the code to this (below) and while all the tweens work here, clicking on the gameobject throws some errors to do with changing the animation curve (probably from changing the method during the tween, since there's no way to do it before using the Begin method).

It looks like it works without error if instead of setting steepercurves and method I set the animation curve directly, but I'm not sure how to set it up so it has the "steeper curves" effect on the curve.

Why do they not work when PlayForward is called individually? That seems like the expected behaviour should be for them to all play as they would normally.

  1.         void AddActiveHeadline(Headline headline)
  2.         {
  3. //              Same stuff. make button, put in place, get positions, etc.
  4. //              int width = uipLine.Background.width;//get width for positioning, tweens//TODO returns INCORRECT WIDTH, not yet resized according to anchors, so I have to do the hardcoded solution below
  5.                 int width = 40 + 6 + uipLine.Label.width + 10;//icon+spacing+label+spacing
  6.                 //slide headlines
  7.                 for (int i = ActiveHeadlines.Count-1; i>=0; i--)
  8.                 {
  9.                         UIPHeadlineButton hdln = ActiveHeadlines [i];
  10.                         if (hdln.transform.localPosition.x > this.TickerBarRightSide)//remove offscreens
  11.                                 RemoveActiveHeadline(hdln);//takes headlines that roll off the screen away
  12.                         else //the tweens
  13.                         {
  14.                                 //get "to" pos
  15.                                 Vector3 pos = hdln.transform.localPosition;
  16.                                 pos.x += width;
  17.                                 hdln.TweenPos.to = pos;
  18.                                 //play
  19.                                 TweenPosition tween = TweenPosition.Begin(hdln.gameObject, 1, pos);                            
  20.                                 //manually resetting the nulled values
  21. //                              tween.steeperCurves = true;
  22. //                              tween.method = UITweener.Method.EaseInOut;//using these two lines throws an error when the tween is viewed in the inspector
  23.                                 tween.animationCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);//not "steeper"
  24.                                 tween.eventReceiver = this.gameObject;
  25.                                 tween.callWhenFinished = "TweenPosComplete";
  26.                         }
  27.                 }
  28.         }
  29.  


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Only one tweenPosition plays at a time
« Reply #3 on: February 16, 2014, 02:22:44 AM »
"Steeper curves" and "method" are both a part of older functionality that was there before the curve was added. You can still use them if you like, but the animation curve should then be a simple straight line going from 0 to 1. What is the error you're getting?