Organizing tweens into an array
Many of the built-in functions of the animation Library accept arrays as well. In this lesson, we will extract our tweens and push them into an array so we can practice sending in arrays to the timeline methods.
Video Transcript
00:00As promised, Chapter 8 is easy, it's simple, we're going to learn a couple of new stuff.
00:06In the process, I just want to regurgitate and practice a little bit more because this is really going to be a not very heavy chapter.
00:15So I went ahead and copied everything from Chapter 7 and put it into Chapter 8b.
00:20And what I want to do next is I want to continue our animation, but instead of creating a yo-yo effect,
00:26what I really want to do is I want to animate each character singularly out of our screen.
00:33So to do that, basically after creating these two tweens, what I'm going to do is I want to create another tween.
00:40So I'm going to create another tween by literally just going underneath here,
00:45and I could even do this after creating my main timeline.
00:48And because we saved our array, what I want to do is I want to first loop through that array.
00:54So I'm going to create here a new array called atweens3.
00:58It's going to be an array.
01:00Initially it's not going to have anything in it.
01:02And I'm going to loop through the previous array.
01:05So I'm just going to go create a new variable called i, set it to zero,
01:09make sure that i is smaller than atweens' length, and that i grows by one in each round.
01:15Now the next thing that I need to do, and I'm just going to,
01:18literally what I want to do is I want to find the actual display object,
01:23in my case the text field, I want to fetch it, or not the tween itself.
01:27So I want to go into my atweens, go to the actual item and ask for the target itself.
01:32I believe that would be the property.
01:34And you can see that we're getting our text fields, and that's exactly what we hope to get.
01:38So this is how we're fetching our tween.
01:42So what I would even want to do maybe even better than that is I'll create another function.
01:47I'll call a private function that will fade out text.
01:54And it's going to accept an array.
01:58It's going to return an array.
02:01And what we're going to go ahead and do is for our atweens3,
02:05I'm just going to go ahead and just call the function fade out text.
02:09And I'm just going to send into our atweens.
02:14And I'm going to go ahead and just copy this for loop,
02:17and drop it back down into that fade out text.
02:20And the next step literally is to go through and actually create,
02:24and I didn't take that with me.
02:27It seems like I didn't take a lot of things with me,
02:29so let me undo that for a second.
02:31Okay, so we're going to take all of that, put it there.
02:35Okay, so let's just run it one more time to see that we're still getting,
02:38okay, so we got a little bit of errors.
02:40That's what I like doing is I always throw,
02:42I always publish every few seconds just to see if I made a mistake.
02:45And you can see that the mistake I made was that I forgot to rename
02:48because I renamed the element inside.
02:50So just fix it really quickly.
02:52Go ahead and check what other errors we have here.
02:55You can see that we have an unexpected closure to this function.
03:00Let's see why.
03:01So this seems like it's in the right spot.
03:05Function does not return a value.
03:06It's saying that we're returning an array, but we're not.
03:08So let's just return for now, return for now an empty array.
03:14And let's move on.
03:15Okay, so let's just run it again.
03:16And by the way, that's exactly the process of debugging.
03:19It's not about doing it right all the time.
03:21It's about like every small change you make, do a quick publish,
03:24just make sure that nothing major happened and there's no errors.
03:28And if there is, just quickly fix them like we just did right now.
03:31So we're all good.
03:32Okay, so we got our object.
03:33The next thing is we want to actually create an array here
03:37because we're going to send out an array, so A out, let's call it.
03:42And it's going to be an empty array in the beginning.
03:45And you definitely guessed it.
03:47We want to take our A out and literally just push our...
03:51Oh, what am I scripting there?
03:53Oops, again.
03:55There we go.
03:56Push, push, push.
03:57There we go.
03:58And we want to push our item into it and then return our item.
04:01And when I'm thinking about it now,
04:03when I'm looking at this fade out text, that's probably not the name.
04:06So let's call this get displays.
04:09Get displays from tweens.
04:13Yeah, that works.
04:14All right, get displays from tweens.
04:17That's even catchy.
04:18Try to say that 10 times fast or don't.
04:21And I'm going to go ahead and just change that copy there.
04:23Get display from tweens.
04:25And really, we don't need this A tweens anymore.
04:27We could go ahead and go back to our TL main.
04:31And we're going to use that property we discovered a short while ago,
04:37which was stagger.
04:38And we're going to stagger too.
04:41We're going to stagger too.
04:42And we want to send into it our visual items that we just grabbed there
04:46because it's going to come back as an array,
04:48which is exactly what it needs to be.
04:50But now we need to set the duration of each animation.
04:52So we're going to set the duration of each animation to be one second.
04:55And we're going to set what properties we want to animate.
04:58And I'm going to go ahead and alpha out our element to zero.
05:03And last but not least,
05:04we want to decide what will be our staggering and our staggering time.
05:09Let's say we'll give it a zero dot two to make sure that it gives it
05:13a little bit of time.
05:14So it creates like this kind of like a wave effect almost on our animation.
05:19And by the way, we could definitely if we wanted to,
05:21we could have definitely added ease.
05:23But if we wanted to add an ease,
05:24we probably would have to separate it and create a tween for that separately
05:28and then go through our animations and do that.
05:30So for example, once we use this tween light to animate everything,
05:34which by the way will affect our speed of everything.
05:36But that's fine because we don't want to wait too long.
05:38So let's run it.
05:39Let's see it in action and let's obviously fix also our mistakes.
05:43So obviously we've done here something wrong.
05:47We're closing something that didn't need to be closed right there.
05:50So let's try it again.
05:51Here we go.
05:52And if I just pull it into our screen, so it'll be viewable.
05:56We'll see that once our animation is complete,
05:58it's going to do absolutely nothing.
06:06Again, that's the whole point of debugging.
06:07So we're going to have to try to figure out when is we have a clue.
06:10One thing got deleted, meaning for some reason,
06:13we're only targeting one of the text fields.
06:15So to try to solve this,
06:16I'm going to go ahead and go to our text fields to see what are we animating
06:21or what are we sending in to animate.
06:23And we could see that by mistake, I left here a zero.
06:27Well, that was a big no-no.
06:29I wanted to put your eye because I want to each time push the next one in turn
06:33instead of pushing the first one over and over and over.
06:36So if I run this one more time, and by the way, it doesn't really matter,
06:39although this probably is not a great idea that we have out of remove children here
06:43because we're not doing any yo-yos.
06:45And actually, we're getting the property way before the animation actually happens.
06:49So it doesn't really matter if we remove the children when we're done.
06:52That's not our issue.
06:53So if I run it just one more time, hopefully this time around, it should work.
06:57Now that I, and there we go, we saw how beautiful that was.
07:01That was insane.
07:07Now, the reason why it was animating in such a weird way is because of that yo-yo
07:11that we have in our balance of our animation, which is actually really rad.
07:16So all I really want to do is how about we just add to our timeline another stagger
07:21and we'll go ahead and stagger into it also the next visual items,
07:26which was our eight tweens too.
07:28And I could just send that into our get displays from tweens.
07:31And this time around, I could set this a little bit longer,
07:34and I could put here a little bit more of a delay just to see what would happen.
07:39And if we run it just one more time, and again, don't forget,
07:41everything will take 10 seconds no matter what.
07:43And that is because our display in general is set to be 10 seconds in that tween
07:48that we did in our tween light.
07:50So there you go.
07:51That was another example of how could we create this kind of cool animation?
07:56Okay.
07:57Now, obviously, we have a problem here because we ideally would love to stop somewhere
08:02and not play all the way through.
08:04But as long as we're tweening all our animation as the tween light,
08:07it'll be hard for us to stop at some point.
08:09We're going to have to take advantage of key frames.
08:12And for us to be able to take advantage of key frames,
08:15that will be the last topic that we have in this course,
08:18which we're going to start talking about labels,
08:20which we haven't talked about at all throughout our course
08:22because it didn't feel like it was very critical so far.
08:25But that's going to be our last, last, last, last topic,
08:28which we'll start immediately in the following lecture.
