Creating a yo-yo effect with vars and data
Although the yo-yo effect is really a TweenMax thing, I thought it would be a perfect opportunity to show you how to create it ourselves using the native capabilities in TweenLite. In the process, we'll learn about scope, variables and the data object.
Video Transcript
00:00All right, last two topics kind of blunder it up together bundled up together into this last topic about tween light
00:08Exclusively, although everything we're doing is tween max as well
00:11Now this is really really powerful the data and the vars that give you access both to your
00:18Variables that you defined in your animation and beyond that how you could add custom variables
00:23Incredibly powerful you could do with it so many different things
00:26We're really just touching the the little bitty bitty what you could do with it because we're out of time
00:32And hopefully you start coming up with your own ideas, which I would love to see
00:35What are you gonna do with those extra?
00:38Capabilities that will enable you to really give personality to your animations
00:43Last last topic in tween light before we move to tween max
00:46But again, everything we're talking here is also tween max relevant and tween max is not gonna be a big topic
00:52All right
00:52So now that we've said that what I want to do is I want to basically kill the animations after that
00:57One time and the way I'm gonna do that is very simple
01:00The way I'm gonna do that is I'm gonna go into my editor, and I'm just gonna teach you a couple of new tricks down
01:07Working with the tween so first of all just again
01:09Where are this is the tween itself because we didn't change anything in the configurations or the scope of our on reverse complete?
01:16So by default that this is our tween now
01:19But the tween itself has a variable called var and that var variable basically contains all the customized
01:25variables that we send inside it basically contains this object that we're sending in to our tween and I could literally go ahead to
01:32my var here and then go to that new well to the on complete that we have right here and
01:38literally just go ahead here and just set it to be
01:41No now
01:43Another what is null you know it might be the first time you ever encounter null
01:46So I want to make sure that you know what it is cuz the first time we use it in all of our courses here
01:50The null basically says that this variable contains nothing or emptiness now you might have heard of the term
01:58Undefined and undefined is basically almost the same as null
02:03But it has a small little difference between them you don't assign undefined into variables
02:08Although you won't get any errors from it the word undefined as the word implies says that it's not even defined
02:15Contrary to that null is something that is defined, but is completely empty
02:19It's not zero. It's not false. It's basically a variable. That's called on complete that has nothing inside of it
02:26Well undefined basically means that there isn't even a variable and that's really the difference between undefined and null in general
02:34Now you'll see it if we're talking about that in general
02:37You would a lot of times see a variable that let's say is called object
02:42But it is a variable that is an object but has undefined in it
02:47And what that actually means is that the actual object itself is not defined not that the variable itself is not defined
02:55Contrary to that you might see inside of a number variable the value null inside of it
03:00Which would basically mean that the number itself was not the value in it was not defined
03:04But its scope was defined so that variable exists, but its content is empty
03:09Contrary to that and you're going to see that more in other languages and not necessarily JavaScript and that's the big difference between
03:15Undefined and null you don't have to worry about it too much and you have two ways to do this
03:19You could or set null like I'm doing right here or instead of that
03:23You could go ahead and really turn it to be undefined by deleting the variable and saying here
03:27I want to delete this internal variable now when you use the keyword delete
03:31It's not going to delete every single thing here. It's only deleting that actual variable. So it's going to delete the variable
03:37It's inside of this dot var dot on complete and that's going to make sure that it's literally removed from our memory
03:43now either or both of them are going to work for tween tween light and tween max and once I do that and I go back to
03:49my my
03:50Application here one more time and I click on refresh. We're going to see that now it's going to do it once
03:54It's going to do the upside down again, and then it's going to stop now
03:57I have you a little error here that this dot var is undefined and that's because I made a silly mistake
04:02It's not var because var is a reserved word. It's actually vars and not var so I apologize for that all that talking
04:10It's called vars. So if I click here and click on refresh, we're not going to get that error
04:14Because it's called vars. It was a very very silly mistake because vars
04:18Well var is a reserved word and I should have even seen it because the color change inside of my editor
04:24So it should have implied to me and told me hey, this is a little bit different
04:27It's the same color like the function
04:29So that that was my bad. We fixed it really quickly. No big deal now before we wrap this up
04:34I want to show you what would happen if you wanted to create this animation and you wanted it to happen
04:38Let's say five times what you could have done is literally just created even in your vars here
04:43Um, you could create an extra variable. I could even go ahead here
04:48Go ahead into my tween itself and just literally just go and add whatever value
04:52I want and it makes sense because one is it might there might be other
04:57Other things you don't know about that it might need and in general
05:01It's not a good practice and it won't actually work
05:03So what you want to do is you're going to wrap up your your variables that you want to send into your object
05:09Into your tween inside of a wrapper which will be called data and then tween light and tween max
05:14Will know don't touch this just pass it on
05:17Once we've done that we could literally just go directly into our element and then just go
05:21We don't even need to use the word vars anymore
05:23We just go into our data and go into our index and we'll have access to that index that we've created
05:29So if I go ahead and click on refresh here
05:30And you know, let me just also mute out all the other traces here so they don't bother us
05:36And i'm just going to comment them out so we could actually see only that data index
05:40I'm just going to even get rid of that
05:42So we'll have your a nice view just of that number
05:45And if I go ahead here and click on refresh
05:47We should be getting only that tracing out every time an animation is complete
05:51And we're going to stick to index zero
05:53But really what we want to do is we want to count our indexes
05:56So I could literally just go ahead here and go back into my code here find that index area
06:01And instead of that just go up here and add here basically a plus plus making sure that my index each time is growing
06:07So this by the way will make sure that it's going to grow my index by one each time
06:12and now I could just go ahead here and add here an if statement if our index equals
06:19A certain amount of index to let's say we want it to be equal
06:22larger than three although we by the way
06:24This is a very good point you could think you're equal and it's completely valid and it would work
06:28But in general
06:29It's always a good idea to do greater or equal to because then you get used to ideas of sometimes if
06:34Somehow for some reason it never reaches point three
06:37It will automatically kill it even if it's at four in our case if it was four
06:41We definitely want to stop it. Although this wouldn't ever happen
06:44It's a really good practice because it will help you avoid common bugs and common
06:50misses when especially when it comes to animation because sometimes
06:53Item doesn't go exactly to three
06:55It'll go to two nine nine and then it'll jump to three dot five now in this case probably not that important
07:00But in our case, you know, it's always a good idea
07:02It doesn't add a lot more thinking to the computer to know if it's greater or equal to three
07:07And i'll go ahead and just delete that on param once we hit the third time and we click on refresh
07:12We're going to get this really nice
07:14Yo-yo effect that's going to happen one. It's going to happen twice
07:17It's going to happen three times and once it reaches that third time or actually fourth time because we had zero one two three
07:23Once it equals three, um, then our animation will completely stop and there you go. And that is it
07:29We're done talking about tween light
07:31It's time for us to move into tween max and in other words in the next lesson
07:36We're going to basically get rid of a lot of the things that we're loading and just get tween max loaded up instead
07:42So we'll see you in the next lesson where we explore a little bit more of the stuff that exists
07:46Extra in tween max that are just not there in tween light. See you in the next lesson
