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

The breakup

Text fields in Adobe flash animate as one unit. If we wanted to animate each character separately, we would need to separate those characters and remove them from the text field, placing them in a text field of their own. One of the challenges with this is the positioning, aligning and configuring of the text fields to sit in the correct position. By the end of this lesson, we will break our text apart and re-position it to sit in the exact same spot as we had it before.

Video Transcript

00:00Alrighty, so let's get started. Again, I already created the logo. So all that's left for me to
00:05do is actually add it to the stage because it's actually on a guide layer. So let me just create
00:10really quickly, I created the variable already. So I'm just going to actually create instantiate
00:15it. And the name of the instance in our library is lmc lead. Now in case although we've been doing
00:21this for a while now, in case you don't know how I got to this name, if you go into your library,
00:27and you find the actual lead item, you'll see that I created a linkage and I gave it the name lmc lead,
00:33which I do that by clicking on right click and then going to the properties. And in my properties,
00:38I just select export for action script, and then I give the class a name. Now it's a virtual class.
00:44So I give it a name of L for linkage, so I could easily recognize it. So that was just a side note.
00:48So let's just continue. So I've created that lmc lead. Now I want to add it to the stage.
00:59And I want to center it on the stage. So I'm going to go ahead and put it in the center area. All right,
01:12now let's give it a roll is just run it to see if it actually is running. And we could see that our
01:19logo is in the center end of the stage, I personally feel like it probably would be nice
01:22was a little bit more up, which I could then just decide that I want to push it up a little
01:27bit more up. So I could just say, let's offset it a bit. So our big copy will be more a little
01:32bit up. And that's a few that feels much better for me. But I would even go maybe a little bit
01:37more just this is really just how you see this feels too much. It's really just a feeling because
01:42that was exactly the center, but it just didn't feel like the center, which sometimes feeling is
01:48what matters. So in this case, this feels a little bit better. But I think 20 was the best.
01:52Okay, so let's continue. I've added it to the stage. Now I'm gonna have a problem when I start
01:57animating. And the reason is is because if I go inside of my inside of my item here,
02:02we'll see that I have your mask. And this mask was created not dynamically. And because the mask
02:08wasn't created dynamically, it's going to mask anything we add to the stage later on. So what
02:14I'm going to have to do is I'm going to have to because we use this mask in an earlier chapter,
02:18what I'm going to have to do is I'm going to have to remove that mask. So I'm going to go
02:21ahead and I'm going to go into that MC lead. And I'm going to call remove child. And I'm going to
02:27call MC lead inside of it and MC mask, which is the name of the mask it by that I'm going to
02:32get rid of the mask, we won't see any difference now. But because I got rid of that mask,
02:37now when we start doing the animations themselves, we're not going to see weird animations.
02:41Okay, so let's continue what I want to do next, I want to create a function because we have your
02:46two text fields. What I want to do is create a function that will retrieve those text fields
02:51and literally break them apart. So each character will stand on its own. So to do that, I'm just
02:56going to create a function and I'm just going to call it break up text. And I'm going to send into
03:02it the movie clips. Okay, so let's start with MC lead.t lead, which is the name of our text
03:11here. So if I double click inside of here. Oh, see, there we go. So our MC lead is our MC lead. There
03:22you go. Okay. Alright, so that was I'm sending my MC lead into breakup text. Let's go into breakup
03:27text and try to figure out Okay, what do we do next? Alright, so private function, let's create
03:31that function really quickly, break up text. And it's going to accept one property for now.
03:38And we're going to call that property a text field. And next thing that we want to do is we
03:46want to retrieve the amount of characters that are inside. So we'll be able to then loop through the
03:51characters and create a new text field that will only have the characters that are actually inside
03:56of that element. So I'm going to go ahead and I'm going to create a charge. And I'm variable,
04:03it's going to be an integer, and I'm going to go ahead into my text t text, and I'm going to
04:07ask for its length. And let me just trace it out. Just to see that we're getting what we're expecting.
04:14And we can see that we're getting six, which is exactly what we wanted to get. We have six
04:18characters and zero to gig perfect. So let's continue. Next step that I want to do is I need
04:23to figure out the boundaries of my text field. Now, the reason is I need I need to find the
04:29boundaries because I want to know how much spacing do I need to have between my character
04:34and the zero position that I will have. You know what, let's start without the padding,
04:38so you'll see what I mean. So I'm going to go ahead and right away and just create a new loop.
04:43And I'm going to go ahead and create call it I'm going to start at zero. And I'm going to continue
04:48as long as we didn't reach chars. And I'm going to upgrade i by one each round. And each round,
04:55what I want to do is I want to basically create a new text field. So I'm going to come here,
04:58and I'm just going to create a new text variable TF for text field. And that's going to be our text
05:06field. I'm just going to literally just go to my text field and create a new text field
05:10each time we run it. So we create a new text field each time we run it. And now before we
05:16even do anything to that text field, what I want to do is I want to set its format,
05:21because I wanted to use the same character size, I wanted to use everything the same as it had
05:25in the text field that we have originally. For us, it's really easy because our text field was
05:31was our fields were created already, they're defined. So all I have to do is to go to my text
05:36field, and set my default text format to the same default text format of the actual text
05:43field that originally was. So that way, we're just duplicating the same features that we had
05:48in the other text field to this text field. The next thing that I want to do is I want to
05:52make sure that my new text field is high enough. So we won't have any copy missing. So to do that,
05:57I'm just going to grab the same height from the original text field. Next thing that I want to do
06:03is I also want to set my width. Now, I don't know what width I need to have in my text field. So
06:08I'm just going to for now just put here for my width, the actual width of a character. So when
06:14we think about it, how are we going to get the actual width of a character? The best way to do
06:20that is to approach the actual character itself in that text field, and just ask it what its width
06:27is. So I'm going to go to get char boundaries. And I'm going to say which character I want from my
06:34first character. And I want to get from that first character the width. So I'm going to pass
06:39over the width, I'm going to have a problem because I'm not adding any padding at all.
06:43So we're not going to align, we're going to have some issues, and we'll have to figure out how
06:46to sort those issues. But we'll figure that out soon. So we set our height, we set our width,
06:52using that get characters bounds. The next step is to actually put inside our text field into the
06:58actual text field. So I'm going to go ahead and set my value to be that text value and char at
07:04and send the actual character in that position into it. Now, the last things that we need to do is
07:11well, a couple of things is we need to set the x and y positions. And we also want to add it to
07:16the stage so we could actually see it. So let's get ahead and do that. So first of all, let's set
07:22our x and y characters, which will be a problem because we don't know exactly text field,
07:27we don't know exactly what position we want it to be in. But we know that roughly,
07:32we want it to be in the T x x position, which is the x position of the text field that was
07:38there already. There'll probably be some padding that we'll need to figure out. But beyond that,
07:42we could also add to it the internal x position of the character just like we got its width,
07:48we could approach a get character boundaries and ask for it for its x position. Okay, now the y
07:57is a lot easier because we're assuming that we only have one field. So I'm just all I have to
08:02do is set my T y, and that should do the trick. Now, we are definitely going to have alignment
08:08issues. But before we solve them, we have to add our child. Now to where do we want to add the child,
08:13we want to add the child to the same parent directory that our text field is in. But we
08:18might want to make it dynamic. So if in the future, maybe we'll want to put it in a different
08:22movie clip. So we so we could. So what I'm going to do is I'm going to go to my text,
08:27although what I could have done is come here and say, hey, var mom, it's you're going to be a display
08:35display object container. Let me just do this right. So we'll import it. So display object,
08:44container, and I could set the display object container to be the t dot parent. There we go. And
08:50that would be the parent. And then I could go ahead and just add the parent right in there.
08:54So I could just go to mom, add child, and then go and add my new text field inside. And if I run this,
09:02it should work, we should be able to see our copy, and we might see it in the wrong spot. Now,
09:07before I even move on, what I want to do, so we'll be able to see if we're actually getting it,
09:11I want to come here, and I'm going to go to my original text field, and I'm going to set its
09:14alpha to zero dot four. So we will to see anything that's new, that isn't with an alpha.
09:20All right, so next step is let's run this just to see what happened here.
09:25So we can see first of all, our copy is literally almost aligned, but we see this halo going on
09:31that that's telling us that it's not exactly and we can see that the copy is getting a little bit
09:35cut. So we really need to tweak this just a bit to get our characters to fit exactly where they
09:41need to. Now, the reason why this is happening, if we look at this, again, we're talking about
09:45padding is because there's an extra space in the beginning that we're not really thinking of
09:50because each character doesn't start exactly from the zero position, there's a little bit
09:54of padding. And what we really want to do is we want to get that padding. So to get that padding,
09:59I'm going to create a new variable up here. And I'm going to call this variable padding x,
10:05and it's going to be an integer. And I'm going to go to my text field again, I'm going to get the
10:09character boundaries. But this time around, of zero, and this time around, I'm going to take
10:15the x position. Now, the reason is the x position of the first character is going to give me the
10:21offset from zero because our text field itself, its beginning would be zero. So if our character
10:26is a little bit after zero, that means that is the padding that we need to use. So if I trace this
10:30padding out, you should see or maybe one or two, we'll see some sort of offset, you see,
10:37so we have your two pixels. So we know that our character is two pixels shifted than where
10:41it should be. So all we have to do now is just take our padding and add that padding to the
10:47position where we need it to be. And that would be probably in our x position, we want to subtract
10:53the padding itself. So we're going to subtract that padding x from our value. And if we run
10:57it again, we should see that we're not going to have a padding issue anymore. But we might still
11:02have issues of our characters just bleeding a little bit more than they should. So to fix
11:07this issue of our characters bleeding a little bit more, we want to also increase the size of
11:12our text field width a bit. So we could actually make sure that our width will include everything.
11:17So if we have a padding in the beginning, and let's assume a little bit of a padding in the end,
11:21so we probably would want to add also twice the amount of padding to our width.
11:27Okay, that makes sense. Beautiful. So we so we added that text padding. Now that we added the
11:33padding, we're more or less we should be good. And let's hope that we don't see any issues with
11:38alignment. But let's see what happens. So there we go. So now we now we can't even see that the copy
11:46is overlapping. And just to prove that the copy is overlapping, I'm just going to go to my car
11:51charge here and just say just show the first three characters instead of the whole thing.
11:55And you'll be able to see now that our character is now we can see that our copy is aligning
12:01exactly where it needs to. And we're perfect. Alright, so if I undo this, and I'll just put
12:07this whole thing the way it is, I just want to do two more things. One is I want to add here
12:12an optional variable, which will be the new mother. So I'm going to put here an app optional
12:18variable that will be let's call it um, let's call it parent, par, par for parent is going to be object
12:29display object container. And by default, we're not going to send anything into it.
12:36And then I'm going to go into my mom's variable. And I'm going to say, if you have a parent,
12:41then do parent, if not, then go to the parent of this object. Okay, this is the or command
12:46basically saying, I'm gonna or do this, or do that, whatever comes back is true. So if null,
12:52null equals false by default, so only if the value here is actually an actual display object,
12:57it will be our mom. And if not, we're going to use our element. And just to test out and see
13:02this actually works, I could just send here, let's say the stage. And if I send in the stage,
13:08and oh, I did that in the remove object by mistakes, let me delete that and go into the
13:13breakup text and set it into our stage. And if I do that and run it again, we should see
13:17that it should go inside of our stage. And then we'll see that our copy is up outside in
13:21a different position, which is exactly what we would expect it to do. So that's perfect.
13:25And if we don't put anything inside there, it should automatically go to the right parent
13:29and use the same alignment ish configurations that we had for our element there. So there
13:34we go. So the last thing that's left for us to do is to test out and see if this function also
13:39works if we send over another item, such as our text field that was in the mask earlier. So if
13:45I come here really quickly, and just select it, and find out what its name is, it's t tagline.
13:51So I'm going to go ahead and I'll just call break up text MC lead, and send inside my t tagline. And
14:00hopefully, when I do the t tagline, we should see the same effect. And just to make sure that
14:05everything is working, because it works so beautifully, I'm just going to set your characters
14:09to show only show the first five, just for testing. So I could see that it's actually
14:14really positioning where we want it to position. And there you go, you can see that our copy is
14:18literally going into the spot that we want it to go. So now that we have our characters,
14:23and we've created our characters, and we set them in their right position, now we have each
14:28character created separately, which opens us the door to start creating animations for each
14:34character separately. And that's exactly what we're going to do in our next video, we're going
14:39to start using our timeline light to create some intricate animations using that this broken up
14:45text that we've just created. So we'll see you in the next video, when we move on to that next step
14:50of this project.

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!