Calling back more in TweenMax
You might have guessed it... with the move to TweenMax there are more controls and more callbacks. In this lesson, we'll explore the callbacks of TweenMax.
Video Transcript
00:00So let's continue where we left off.
00:01And there's a few problems still here.
00:03And one is the thing that is on the top
00:06stays on the top, which makes it a little less interesting,
00:09at least to me.
00:11Wouldn't it be nice if once it gets to be the smallest it can,
00:14we would somehow plop it all the way
00:16to the bottom of our screen?
00:18So let's try to figure out how to do that.
00:20So I'm going to stop my app.
00:22Now, the first step that I want to do
00:23is I want to add an onComplete for our function
00:27to see when does our onComplete get called.
00:30So I'm going to call your onComplete.
00:32And you know what?
00:33Even before that, I'm going to make sure
00:35that I'm only going to create one item.
00:37And back to our onComplete, I'll call this onRound, onAnimComplete.
00:46And I'll just create here quickly that function, onAnim.
00:54Let me just copy that.
00:58And I'm just going to quickly just trace here.
01:02I'm done.
01:04I'm just going to run here for a second.
01:06And we'll see that once it reaches the bottom,
01:09it reaches the top, it reaches the bottom,
01:13it will never complete because we're creating here
01:15an endless loop.
01:16So complete doesn't really mean anything anymore for us
01:20as long as we're in an endless loop.
01:22So we're going to have to use something else.
01:23We can't use onComplete anymore.
01:25So instead of using onComplete,
01:27I'm going to use the onRepeat.
01:29And onRepeat is going to call each time a round is completed.
01:32So I'm going to just call it onRepeat.
01:36And we'll run this one more time.
01:38And we're going to see now that our animation is done.
01:41Our animation is done.
01:43Our animation is done.
01:44So basically, every time it finishes a round,
01:47well, not a full round, every time it
01:49finishes its original onComplete, it's going to call.
01:52But really, what we want is we want
01:54to recognize when are we actually really completed.
01:58So to figure this out, we want to first of all,
02:01we want to pass through our tween itself
02:04so we could approach it.
02:05So to do that, I'm going to create another prompt.
02:07I'm going to call the callback extra parameters using
02:10the onRepeat params.
02:13And I'm going to send into it the string of self,
02:18if you recall, that will send in the tween that
02:21is actually running.
02:22So that's going to be the tween max item.
02:24That's going to come through.
02:26And what I'm going to do here is I'm
02:28going to try and find one of the properties of our tween
02:32that will give us a little bit of information about where
02:34is it at in its phases of its repeat.
02:40I'm going to use the reverse, because the reverse is
02:44going to return a Boolean value.
02:46I'm not going to switch it around.
02:47What I want to do is let me just put it right inside there.
02:51And if we run it, we'll get the value back telling us
02:53if it's going forwards, if it's going backwards.
02:57And in our case, we're seeing that the reverse is not
03:00coming back.
03:00And really, what we're getting out of this,
03:02and maybe I'm not finding the right property,
03:04but the problem that we're having here
03:07is that because we're creating this,
03:09this is not a property that we set.
03:10So it's not returning to us what we would hope for it
03:13maybe to expect.
03:14We need to figure out a smarter way to deal with this.
03:17So to create for a smarter mechanism,
03:21what I'm going to do is I'm going
03:22to use a property called data.
03:24Now, the data object enables me to basically add properties
03:27that would then be accessible to my tween.
03:30So what I'm going to do is I'm going to add your counter.
03:34And I'm going to set the counter at 0 at the beginning,
03:36or maybe even 1 at the beginning
03:37for the first animation.
03:39And I'm just going to go to my value here
03:42and just going to see that I have access to it
03:44to my counter.
03:45And if I run this, I'm just going
03:46to hope to get the value of 1 each time.
03:49And by the way, I see here that I have a small error,
03:50so let me see what that error is.
03:53So first of all, it's telling me that the term is undefined.
03:55And my guess is that I actually have
03:57to create a new object for my data.
04:00So I'm just going to set here a new object.
04:03I'm going to call it counter.
04:04Going to set the value to be 1.
04:06Let's give it a try again.
04:09Here we go.
04:09So now we're getting that 1.
04:11Beautiful.
04:12So now the next thing that I want to do
04:13is each time a cycle is complete,
04:15I'm actually going to start it at 0.
04:17Each time a cycle is complete, I'll just go to my counter
04:20and I'll bump it up by 1.
04:22Now, by bumping it up by 1, I'm going
04:25to know basically what cycle am I in.
04:28So I could be in cycle 1, cycle 2.
04:30Now, the value doesn't really matter that much.
04:32So if we wanted to, we could reset that value each time.
04:35But in my case, I'm going to keep it here just for fun.
04:39Now that I know the directions, the next step
04:42is a little bit easier because then I could just
04:44ask if my value that I have there,
04:46a molder 2 equals 0.
04:48That means that equals 0.
04:51Or even just by putting that, it's going to be a value
04:54because it could or be 1 or be 0 because basically it's
04:56the leftover by division by 2 when I set your molder 2.
05:00So if it's 1, this is going to be true.
05:03If it's not 1, then it's going to be false.
05:05So I'm going to put here a small little else here.
05:09Let me just trace out here.
05:10If we're at 1, that means we're still in a loop.
05:13So halfway through, new round.
05:22And then when we run this, we should get the first time around,
05:24we're halfway through, then we are in a new round,
05:28halfway through, new round, and so on and so forth.
05:33OK, perfect.
05:34So let me now bump up now.
05:36Now that we have a way to access the repeat duration,
05:39let's bump up our amount of items.
05:41Let's not go all the way through.
05:43Let's add here 10.
05:44And now that we have access to it,
05:46we can now start figuring out, OK,
05:47what do we want to do in case we're all the way on the bottom?
05:51What do we want to do if we're all the way on the top?
05:54Now, I'm going to pause this video.
05:55And in the next video, what we're going to do
05:57is we're going to go through and try to figure out,
05:59for one, how about whenever we're halfway through,
06:04we're going to make sure that it's in the top of the stack.
06:06Well, actually, how about every time it reaches the bottom?
06:09We're going to make sure that it's
06:10in the bottom of the stack.
06:12And every time it reaches to the, well, really,
06:14that's it, really, right?
06:15So in the next video or the next lesson,
06:17we're going to continue on from this point.
06:19And we're going to make sure that we're going to actually
06:21change its actual position, actual layering, actual stacking.
06:26So let's get started.
