02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Animating from and to places

The basic animations, TweenNano.from() and TweenNano.to(), enable us to animate element properties that are code accessible. The to method enables developers to move elements to new places while the from method defines the starting point of the animation, animating the object to the origin position. By the end of this lecture, you will know how to work with the TweenNano.from() and TweenNano.to() methods.

Video Transcript

00:00Alright, so let's start animating. I'm just going to delete everything inside of chapter one.
00:04I don't need to import tween.nano again, so just make sure that you imported
00:09com.greensoc.tween.nano. And once we've done that, all we have to do is go to the tween.nano,
00:16which has two main properties for animation. One is called to and the other one is called from.
00:23Let's start with the to, which is the most simple one. Well, both of them are quite simple.
00:29The to function expects us to tell it what do we want to animate. So let's say we want to animate
00:33our text field, which is called T names. And again, don't forget that you have to make sure
00:39that your fonts are embedded if you're animating text fields, because if not, those animations
00:44wouldn't work. All right, the next property is the amount of time which I'm going to set
00:48it to be one second, but the duration could be modified and be instead of seconds to be frames.
00:56Now what I'm saying frames, what I mean is the frames per second, just like our flashline flash
01:02by default runs on frame. So if we look at our timeline, we'll see that it has frames. And we
01:06can see that our current default setting is 24 frames per second. So if I wanted to, I could
01:12have set it to be 24 frames instead of one second, but then I would have to change the
01:17default property that would actually say to say to tween.nano that we want to use frames
01:25instead of using seconds, we will look into that as we explore tween light, but I'm going
01:31to treat it as seconds, we're not going to go into different configurations that we could
01:35configure in the tween light, I really want to focus mainly on the things that are different
01:39in tween nano than tween light, besides seeing the basics of how to animate something. So
01:44let's see it animate. So the first first property we said was the target element,
01:48which was our text field. The second is the amount of time which by default is seconds.
01:53And the third is an object that we want to create a dynamically created object,
01:57which we could send to it properties, any property that belongs to that specific object.
02:02So as long as it's a numeric property, this would automatically update it. And it doesn't
02:07have to be a visual element. But obviously, if we animate a visual element, we would actually
02:12see that animation taking place. So for example, if we wanted to, we could have set the alpha
02:18to alpha to zero, which the alpha ranges from zero to one, which where zero is completely
02:25invisible. And one is opaque. Now, if I just run this application, the way it is,
02:30I will see that my my tween will automatically automatically disappear. So if I run it again,
02:37you'll see that it automatically disappears. Okay, so the next step is once we created
02:44animation, by the way, we could add as many properties as we want. So maybe as we wanted
02:49to fade out, we also wanted to move a little bit to the left towards the logo. So we're
02:55going to move it a bit to the left towards the logo. So I'm going to find its position.
02:59And by the way, let me show you just a quick little cheat. So instead of one is like before
03:05even I put a cheat, I want to set the x and what x I want it to be, I want it to be the x
03:10names x minus 30, because I want it to be less than the current position that it is by 30.
03:15So if I run this, we'll see that it's going to animate slightly towards my logo. Let's see
03:20this one more time. So you see how the animation is slightly moving towards my logo. And this
03:26is enough for me, I kind of like this little fade event. By the way, and if we're already
03:32talking about it, a nice quick cheat here is instead of putting a number to put a string
03:37and inside of it minus 30, and the package will automatically detect that it's a string
03:42and subtract 30 from the from x. Now if you put a number minus 30, it would just take it
03:48to the position minus 30. But if you send it as a string, you'll see that it's going to create
03:52the same exact effect, only with a lot less typing, which I'm personally a big fan of
03:57typing less. So I really like that kind of small little behind the scene cheat.
04:02So that's it about two. What I want to do next is I want to look at the from.
04:07Now the from works very similarly, only the difference is that from instead of going from
04:13the last instead of your, you're going towards something. So the values that you're putting
04:19here is where you want it to go. The values that you will put inside and in the in the case of
04:24from is the values that you want to put when you as the first frame, and they will end in the
04:29position that it's currently at. So when we do the from, I'm going to go to the MC logo,
04:35and we're going to parallel animate it. But this time I'm going to do it for one that five seconds
04:39instead of one second. And I'm not going to change it. Oh, I'm going to change the alpha.
04:43I want it to start from zero and this time instead of ending at zero. And I'm going to
04:48change its x position again as well. But hey, let's let's change the y position just to show
04:54that you could play with various different or even let's let's rotate. So we're going to start
05:04with a rotation of 90 degrees. And by the way, I'm not sure even if rotate property exists,
05:08because I'm my memory is short. And by the way, this could definitely happen to you because
05:12there's a separation between the object and the element. So all I have to do is go to MC logo.
05:17And I'm going to have to declare it and then we'll delete it. I'll do this really quickly.
05:21MC logo, it's a movie clip. And then I'm going to go to my MC logo. And I'm going to search
05:26for rotate. And there's no rotate, there's rotation. So there you go. That was my mistake.
05:32So I'm going to change it to rotation. And that was just for me to cheat and see the actual hints
05:37that come inside of flash. So now that I set the rotation, by the way, again, it's going to start
05:42rotated is going to start in a position at visibility zero. And then it's going to animate
05:47through and it's going to take it a second and have to reach the actual position that it's at
05:52right now. So I'm going to animate that. Oh, let me just stop that and start it over. Let me just
06:00save it and run it one more time. And there you go. You saw that animation. Let's do it again.
06:06It's also rotating. It's also fading in. And the starting position is really that critical
06:12position. And so so with to the two, we started from where we were and then we moved 30 pixels
06:17back. And with the from we said it, where do we want to start from? So for example, we might want
06:22to also set again, for example, the exposition this time to be plus 30, or just 30. Once it's
06:28inside of a string, then automatically tween nano and would recognize that it's actually plus 30
06:36to whatever it was. Or let me let's put a much larger value here. So plus 100. Do you see so
06:47the animation starts over from much earlier, and then it's like rolling in with us. It's a it's
06:53much harder to see it mainly because it's rotating at the same time. So if I put here like even 200,
06:59then you'll see that it's a much more drastic animation, that plus is really that that critical
07:05is not that critical. But you could put there a plus or not very, very cool. So that kind of like
07:10gives you that positive flow. So it's kind of like coming from within the text itself rolling
07:15into its position, I really like this, I would even maybe make it a little bit larger. So it's
07:20like a nice, full loop, obviously would be really nice if we actually ended in a position where our
07:27logo was right there in the center. So what we might want to do is maybe start off with our
07:33position to be in the absolute center of the stage. So I would just go there and just set that
07:38to be in the center. So we're going to align it to the stage and set it to be in the center,
07:44because this doesn't really matter. We're not we're it's not going to be visible at first. And
07:49we could see how it rolls into its position. Very, very, very cool. Okay, so so we've seen the
07:56basics of how to work with tween nano. And in the next lesson, what I want to do is want to talk
08:02about and just really globally really quickly about the things that you want to be aware of
08:08that are different in tween nano versus tween light.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!