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

Adding The ENTER_FRAME event

Now that we know what's happening behind the scene when enter_frame is called, let's add that enter_frame into our roll over. In other words we want to listen to our enter_frame only when our user mouse is on our movieclip.

Video Transcript

00:00We're back to our rollover states and if we just run it for just one second
00:05Let's see what's going on
00:06So just as a reminder when you roll over the copy comes up comes and appears and when we roll off it goes away
00:12So we could see that in line 22 that the copy comes in and in line 26 where the copy comes out
00:18But what we want to do next is we want to leverage that enter frame. We want to leverage that
00:23Update frequency and to do that what I want to do right now is I'm going to comment out for a second here that line
00:29That actually created our rollover
00:31And what I want to do now is actually add an event listener
00:34That actually tracks every time when and when a frame is entered into or more accurately every time when and we're going to listen
00:42To MC's home as well because we've done that to all the rest as well
00:45So we're going to add an event listener this time around the type of event is going to be event event and the end
00:52It's enter frame. All right, so event dot enter frame
00:57And
00:58The next step is we want to again put your listener. So I'm going to put your listener and call that listener on
01:05Enter on
01:08Well, let's write on roll
01:11enter
01:12All right, so we know that's an only on on roll it happens and we do an enter frame
01:18Now the next thing I want to do is I want to create a new function
01:21And that new function is that on enter?
01:24on roll enter so function on
01:28roll enter
01:31Did I type that? Yep. Okay on roll enter and it accepts one parameter, which was the event type
01:37And it doesn't return anything because events don't return anything event listeners don't return anything
01:43All right. So if I put here a little trace, we're going to trace it out and here I am
01:48This is fantastic. And if I run the application there nothing's going on
01:52There's nothing tracing out as as soon as we're going to roll over the icon then there's going to be an endless
01:59Here I am, but it's never going to stop now
02:01We want to make sure that anytime the user rolls off it will stop to do that again
02:06We're going to go full screen again
02:08And what we're going to do is make sure that on on out home
02:11We're going to make sure that we remove the event of add event listener
02:14So I'm just going to grab my add event listener copy it
02:18Take it to line 27 and in line 27 instead of adding the event. I'm going to make sure that I removed it
02:25Remove event listener. All right in this way. I'm kind of validating that I'm and I'm adding the event. I'm removing the event
02:35Okay, and now just to make sure that it's working what I want to do is I'm just going to trace out here just a little
02:42Separator line it's a great way to practice on this when you're looking to see if something's working or not
02:46Alright, so I'm rolling over and now I'm rolling over in my here
02:50I am is right in front of me and as soon as I roll out
02:53I'm going to get that blocking code telling me I just completed in on
02:58Home out which is the roll out and
03:00The event was terminated and we could see that is not continuing and if I go on it will continue and off
03:06And we could see that our logic now is working
03:08So brilliant now the next step that really is left is just to figure out the logic of how to add the copy
03:14step by step into our
03:18Into our
03:21Into our
03:23Logo rollover state. All right, so we're inside of our on roll enter now
03:28We know what text field you want to change already have that parameter right here now
03:32What do we want to add into our text field? Well, we want to add into our text field one parameter
03:37We actually want to add
03:39Only one character out of the home page
03:41So every time that there's an enter frame every time that the frame changes we want to add one character
03:46How would we do that?
03:47So we obviously we wouldn't we wouldn't just copy the whole copying because if we would then it just wouldn't work the way we want
03:53To so but let me just keep this for just one second
03:57And what I want to do is I want to see I want to get out using traces again to try to fetch
04:02First of all try to fetch how many characters my MC home text home has and that's relatively easy
04:07I could go directly to my text property and just ask it how long it is
04:12So I'm gonna grab my text itself, which is a string and then I could ask for my string
04:18To get its length
04:20Now if I run this I might it might
04:25If I run this we'll see that my length is zero, right?
04:27There's not nothing in it because there really isn't anything in it
04:30Now the next parameter that I need is I want now that I know that my length right now is zero
04:35I want to actually get only the character in position zero
04:39Because my length is actually
04:41Let's see how that works. So if I take my home copy
04:45Now I want to get only one character so we could see that we have a one of the first methods of the string
04:51our char at and char at accepts one parameter, which is a number and in our case
04:56It's zero, but we're just gonna copy that length parameter because that length is exactly what we need
05:02Because as long as we have no
05:04As long as we have nothing, right? There's no copy and it'll go to position zero
05:08As soon as there's one thing in it, it'll go into position one. So it really correlates beautifully
05:13So right now if we're running we should get oh we have a little error here. Let's see what's wrong
05:17We didn't close our bracket, right?
05:20And I could already see that but if I didn't I could have always used that shortcut that we learned about which was the command
05:24Comma and we could see oh
05:27Okay, this is not well. Yeah. Yep. Something's missing here, right? Or if I would do it again, then and it's very very useful, right?
05:34So I'm gonna close that parameter and yep, everything looks good now. And if I run it
05:38We should now when we roll over we're gonna get H all the time, which is not exactly what we want
05:43But we're getting very warm. So now if I just grab this whole line of copy
05:47I could just add this whole line of copy to the current text field
05:51The way that the preferred way of doing that in ActionScript 3 and historically in ActionScript 2 the most common way of doing it
05:57would be
05:59Text plus equals and just adding our copy which in our case is
06:03This copy that we've just seen in our trace, right? And this would work perfectly fine
06:08But we don't want to do it that way because it's more taxing on the system
06:12And we don't want to make things more taxing on the system
06:15And the way we go around it is by typing saying that's called append text, which is a method
06:20So instead of adding the text directly, I could do append
06:24text open around
06:26Because it's a function and then close it around bracket
06:31Now you might say you might be asking me and I could ask that as well
06:36Ben, why is our text home not showing us any hints?
06:40It's kind of hard working without getting that those hints
06:43The reason that it's not is because currently we're oh
06:46We haven't digged into yet the object-oriented world where we could hard code the value of text home for flash
06:52Currently we added it all in the stage and because we added it all on the stage flash doesn't really know what type of variable
06:58Text home is while it's compiling before it's compiled more accurately
07:03So flash is treating as just a property that it doesn't really know
07:07exists really and
07:09Because of that it doesn't really know what hints to give us now if we forgot something we could always go to the help
07:14But probably one of the easiest ways of just of just testing something out to look for text
07:20I know it's a text field
07:21So I'm just going to create a variable called text field and I'm really not going to use it all the reason I'm using
07:26Is just to be able to get to that t dot and to be able to look for the property that I'm looking for
07:31In this case it was a pen text, which I knew already, but it's a great
07:35This is a great way to
07:38Refiguring out if you forgot where something is what the name of it is you just knew that you know
07:42You're working with a movie clip you're working with the text field
07:44You're working with a sprite or any other type of visual or any other type of object
07:49It's a really nice way of getting that information until we get to that place where we could build things in a more structured way
07:55Which would give us all those hints?
07:57automatically so far so good and now applications should work, but we have one problem that might not be very noticeable, but it's a problem even
08:05Though our application is rolling in and it's really doing a very very nice job with that
08:10We have one problem our animation. Although is quite cool
08:13It our enter frame is still running even though nothing is going on and to prove that I could just put here a little trace
08:20here and
08:22Something is going on. It's just not something that we could see
08:26so
08:27Stuff are still going on even though nothing should happen, and it's really important especially when you're working with enter frames
08:33It's to terminate them as soon as you don't need them, so let's see how could we terminate?
08:38Terminate our enter frame when we don't need them and the way I'm going to do that
08:41I'm going to go the line exactly underneath my append text and now when would be the scenario where we don't want to add anything
08:47Else we want we don't want to add anything else when the value of our text field
08:51Equals to the value of our text of our home copy right?
08:56So that if we put that into a programmatic
09:00Statement that would be if our home copy equals to our text home. I'm just going to copy that as well and
09:10Dot text and
09:13Here we go. Oh well. We want to do something right so what we do we want to do
09:16We actually want to remove the event so in the case that everything here was completed
09:21We want to add here
09:23Removing it's the exact same remove that we've done and our roll out
09:27Alright, so let's just put here again a trace to see that really when we're done working with us
09:32It's good to complete that it doesn't continue running and if I roll over we'll see that it added those four characters
09:37And at the same time it added those four stars and it then completed and it does not continue
09:43So it only runs while there's something to do
09:47Perfecto well
09:49Next up what I want to do is I actually want to show you how to actually add our roll out
09:53but I want you to maybe take it make an attempt of doing on your own because
09:57it's one of the best ways to practice these type of stuff of just
10:01Trying it on your own now
10:03I'll give you a little bit of advice is one thing that you want to make sure is you want to make sure that you're
10:07Always removing the enter frame that you're not using so when you do your on out instead of
10:14Removing all the text immediately what I want you to do is the reverse animation
10:18All right now we're gonna take a pause I
10:22Recommend take a pause and just go into your flash and try to figure it out if you can't or when you're done
10:28Move on to the next video, and we will do it together

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!