Getting the tweens of an object
Getting the tweens of an object in TweenMax is really easy. All you need to do is use the method getTweensOf. In this lesson, we will see how and when you would want to get all the tweens of an item.
Video Transcript
00:00Alright, so what I want to do next is what I really want to do is I want to take all this tweens
00:05And I want to actually wrap them inside of our create circle
00:08So I'm going to even change the name to create an interactive circle maybe or create here an upper function
00:14Which would probably makes more sense in case we want to create some there are not interactive
00:19So the first step first first step is I need to be able to use a shape that's interactive
00:24Which shape is just not good enough anymore because it's not interactive so the next element up
00:29I'm going to go for the sprite and the sprite has that
00:34Interactiveness and it has the capability of drawing so that solves all of our problems of step one. I'm just going to
00:40Run through any place that's written shape and just update it to be
00:44Right instead of a shape. I'm just going to save it run it for a second see that everything is working and it is
00:50Beautiful next step is I want to create a new private function
00:54And I'm going to call this create
00:56interactive
00:58circle
00:59Alright, so I'm going to create an interactive circle the properties that I'm going to get are the exact same properties
01:05We might even need a few more, so I'm just going to pass through the same elements
01:10I could definitely have it return a sprite as well in case we want to do another something with the element and
01:16What I'm going to do next I'm just going to grab everything
01:19We've done here and move it into this internal function
01:22So theoretically this should still work and all I have to do is just call our function here
01:28Oh, and by the way, I'm going to have to send this information in right?
01:32This is the information that we had in our call circle and instead of that
01:36We'll just pass through our values that we've created already
01:39So if I run this one more time and let me just make sure that if there's any error that we're going to fix
01:43It so we have here an error that's telling me this is unexpected and that's because we're not closing our round bracket there
01:51And putting our semicolon and let's just make sure still that we're and here we're going in here
01:56We forgot to actually return our shape. So I'm going to return
02:01Our shape as well bugs happen. Don't worry about it. Just follow whatever your bug message is telling you
02:07So it told us it's something there and we easily notice it and by the way speaking about that if you don't easily notice it
02:13That's completely natural. It might take you 10 minutes
02:16But over time as you become more secure with your development, especially when you get used to certain languages
02:23It's thinking easier and easier. These the buggers are incredibly helpful because they give you that range
02:29It's not always the exact same line that it tells you but it's a lot of times and most of the times very very close to
02:35It so you don't have to go too far to find your bug
02:37It's one of the easiest things in programming the moment you get used to it. Obviously until then it's a nightmare
02:43Alright, so let me run this one more time and we can see that our element is definitely working
02:47So next step that I want to do is actually for my interactive object is I want to actually add some interactivity to it
02:54So to do that, I'm gonna add a new event listener and we're gonna use the mouse event and we're gonna call it click
02:59And we're gonna call it on enter
03:02Enter interclick for interactive click. Okay, and I'm just gonna create your a new private function on
03:09Enter click which is going to be a mouse event and
03:16The next step which is a huge problem for me right now is right now
03:20I don't know what is the actual tween that is going on, right? I don't have direct access to my tween
03:27I need to have a simple way to actually detect or find out where is where what?
03:35Tween is going on
03:36So to be able to solve this problem
03:39There's a few ways for me to do that and literally my problem is if I want to change something in my tween
03:43I first have to have access to the tween
03:45I already do have access to the item that was clicked on by calling the e dot target
03:50And if I on my click we'll see that we're gonna get that sprite
03:54The only problem is is we want to have a reference especially if we're going to
03:58Dynamically create a lot of animations at the same time
04:01We're gonna have to have a wait to reference our tween as well
04:04So I'm gonna approach my tween max and I'm gonna refer to our and I'm gonna find that property here
04:11Get all tweens is all the tweens, which is not really what I want
04:14But what I really want is get the tweens of a specific target, which is our shape which in our case is e dot target
04:21Now let's see what we're gonna get back here with our trace
04:24So we're gonna get a reference to our tween based on the object itself
04:27And we can see that we have one object our tween max object coming back, which is perfect
04:32All that's left for us to do is check to see what is its status is it paused right now?
04:38So let me just for the sake of hard typing
04:41I'm gonna just create here a new tween max variable and I'm gonna set that value that we just got back
04:49to be that tween max variable and all that's left for me to do is to go my to my tween max and
04:56Actually get it's is it paused and I'm just gonna trace this out
05:12Let's see what value we give back. First of all, my mistake here is that it actually is an array that comes back
05:17So I'm gonna have to approach the first item because I know there's only one animation going on
05:22so I'm just gonna get that first tween out of it and
05:25Next as we'll see that we are paused that it's true. So if we are paused we want to unpause
05:30So do you remember we did this also with our?
05:34Our tween light version all I have to do is just pass into our paused function the opposite of paused
05:40So we're basically switching it around
05:43So if I click here
05:44You'll see that it's gonna start and I can pause it at any point and I can continue at any point
05:48So every click is gonna switch around between the pause and unpause condition
05:53So now that we've got our interactive element configured all that's left for us to do is to maybe
05:59Dynamically add a few of these elements. So not even dynamically
06:03How about I'll just add three of them, but I'm gonna try to add it a little bit more randomly
06:08So first of all, I already I kind of gotten myself a couple of colors already predefined or you know
06:14What let's do this in a little bit more fun way. So I'm just gonna comment all that out
06:19and I'm just gonna create here an array with colors and
06:25My colors array is just gonna contain a couple of colors that I'm just gonna paste in. So that's my first color and
06:33These are just some of my brand colors on zero to geek too because it's just colors that work well together
06:40What am I doing?
06:42There we go
06:43We're gonna have your gray
06:48And last but not least one more color which is gonna be a type of blue
06:54So I'm gonna make sure that I'm starting it as a hex at the next little value beautiful
06:58So now that I have my colors here, I'm gonna just loop through those colors
07:02So I'm just gonna go through for each bar
07:06CLR for color which is gonna be an integer in a colors
07:12I'm just gonna trace out making sure that I'm actually getting a color as some some number e value which I am
07:18Beautiful. So next up for me is to actually go through and actually call my create method here
07:25It just passed through first of all as our first parameter the actual color
07:29CLR that we're getting here here. I want to create something random
07:33So I'm just gonna make sure that we're gonna start from at least the radius of 25 plus Mac math that random
07:39Times 50, let's say let's even times 100 and I'm just gonna math or I'm gonna turn it into an integer
07:46Making sure that it's never gonna be a there's gonna be a floating value here
07:50So it's gonna be between zero and a hundred and the same goes for our position on the stage
07:55What I could do is just take that stage with but instead of that just duplicate it with math
08:00Random making sure that it's gonna be in a completely random place each time
08:05Grabbing it as an integer and I'm just gonna you know break a line here because it's prior
08:09We're going out of our screen already probably and I'm gonna do the same thing for this one integer
08:15And we're gonna take that wrap it into an integer. But before that we're gonna duplicate it by math times
08:23random
08:24Math random not math times random. So let me just run it for a second see that everything is working
08:29I think that should do that that should do that that should do that. So if I run this one more time and
08:35We should expect to get three circles here. And once I click on any one of them
08:39They're gonna start animating
08:41But really I personally would like the default to actually be a little bit in less. Uh, I
08:47Would like the default not to be paused. So I'm just gonna comment out that defaulting property
08:53And before we wrap up this video, I want to change this for loop even a little bit more
08:58So I'm just gonna copy this because what I want to do is something a little bit different
09:02What I want to do is I want to just create here some sort of a loop where I could crew even though
09:07I'm using this X amount of colors that we have up there. I actually want to run it for 25 times
09:12So I'm going to create her in four of our I integer equals zero I smaller than 25 I plus plus
09:19And let's see. What is what we're gonna pass through in here, which I don't really need that
09:24But I definitely do need this and the only value here that would have to change is making auto my color random
09:32So to do that, I'm gonna go to my a colors and I'm gonna go to a property here
09:36But for that I'm gonna call math dot
09:39Random again, and I'm gonna duplicate that by our a colors
09:44that length and
09:46I'm gonna make sure that I'm gonna turn that into an integer and by doing that by the way
09:49I'm gonna make sure that the value could only be between zero and one less than our length
09:54Because it would never reach all the way to the top that the problem and by the way
09:58It actually could reach it if math ever was one which could happen
10:02So to make sure that that could never never happen you could subtract from your value minus zero dot zero zero zero zero
10:10One if you're worried about it, but it's I'm not very worried because I've never seen it actually order one
10:15But it could be you get a bug that would happen once in a billion years. Okay, so let me just run this out
10:21And if we run this out now
10:24We'll see our two out twenty twenty elements there on our screen
10:29And we can very easily if we want to change the amount of elements if I wanted to have your a hundred elements
10:34We could have a hundred elements now our only problem with our animation right now is that they're all
10:40Breathing and flowing and growing and changing at the exact same time, which is a problem for us
10:46But we're gonna solve that in the next video and continue creating this kind of a morphe and animation that we have going on
10:53Right now and by the way one of the I'm noticing a problem that I'm having here is that I would expect
11:00Elements to go all the way to the bottom, but for some reason they're not
11:03So if we look at this to try to see what is the reason why why would that be?
11:08If I'm gonna look here at my this is my x and y which is for the x we have no problem
11:13We could see that in our x we're going all the way through our screen
11:17But for our y I'm noticing that we're only going halfway out of the screen
11:20So if I look into my code, I'll notice. Oh, I made a mistake here
11:23I actually deeply divided it by two here
11:25So I would delete this in our in the scenario and now we're going to take the full breath of the stage
11:31Which is what we would hope for
11:32We still have a problem is that they're breathing all at the same time and I would want each to come in and out
11:37In a different time and we'll solve that problem in our next video when we learn about another tween max property and capability
