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

Overriding built in methods

Not only methods we create yourself can be overridden. Actually built in methods can be overridden as well. In this example we will show you how to override the play and stop methods of the movieclip.

Video Transcript

00:01So going into the dog class, and we could see that in the dog class itself, we have the touch, and just as a reminder from previous exercises,
00:10and let me just make the screen larger, oopsie, make the screen larger.
00:13So we had our animal, and our animal already had a click event, and that click event triggers the touch.
00:19And thus in our dog itself, whenever it's touched, you'll see that it will bark.
00:24So if we run our application, we'll see that when we click on our dog, it will bark.
00:28There's a few other animations going on as well, such as the dog leaving our screen.
00:33So we probably want to remove that from the animal.
00:36We want to remove that enter frame, which we have your on click move.
00:40So I'm going to just comment out, making sure that when its animals are clicked, they're not going to move anymore.
00:47So if I click on my dog, we'll just see that he's happy to see us and that he is barking.
00:53Very cool. So now let's, instead of have a trace of him barking, let's just go to that movie clip and just play it whenever our dog is being touched.
01:02So if we run our application again, we'll see that whenever I click on it, the dog is happy.
01:07He wags his tail. Now he wags his tail forever. Probably not the best idea.
01:12We probably want to give it some sort of a timeout.
01:14So maybe once it reaches and completes a loop, we'd want it to stop.
01:19Now, one of the problems that we have right now is that we don't know how to know when it's supposed to stop, right?
01:25Because we're not really, we don't have a stop in our frame.
01:29We could add an enter frame. We could add in an event listener to try to figure out when we enter a specific frame.
01:36But probably the easiest thing to do is to go back into our timeline, go back into our dog and just add another frame.
01:42So if this was our dog's frame, or maybe we'll call this body.
01:49So we could add an action script layer. And I know that usually I tell you not to put code inside of the timeline.
01:56But in this case, we could do a small little exception.
01:59We're just going to put here a stop. And really the reason is because it's the easiest.
02:03And if it's the easiest, that's probably the best way to do it.
02:06We just want the dog to wag his tail once and go right back to the beginning.
02:10So let's give it a roll. Let's see if it works.
02:12If we run our application one more time, this time around, we don't need the stop that I've integrated into line 7.
02:18Because it will stop automatically. Because we already added that stop in the first frame.
02:24And now when we click on it, we'll see that the dog will wag his tail once.
02:27And again, it doesn't look that great, right? Because the dog is only wagging his tail once.
02:32And you would think that maybe it'd be nice if the dog could wag his tail two, three times.
02:37And again, even though this is not very much encouraged, what we could do is we could override maybe that stop.
02:45And maybe not stop it always. Let's give it a try and see what happens.
02:50So what I want to do is I want to override our function. So I'm just going to write here override.
02:56And our stop function is actually, it's a public function.
03:00And our public function, what is it? It's a public function that is called stop.
03:05And even though this is not one of the functions we've created, we could override that function.
03:10Now, we could ask it to go to the super just as we've done before and just ask it to stop.
03:16And even though our animal doesn't have a stop, our animal extends, the movie clip which extends on and on and on in the chain until it'll reach that stop.
03:25So what we're actually doing is we're making sure that we're stopping through our layer here.
03:31And we could trace here just to see that it actually works. So trace, we are here and it should stop in track.
03:39And we could see that we are here for both the dogs and we're in perfect, perfect sync.
03:44So what we could do next is what we really want to do is we want to create some sort of a layer where maybe the dog tail will wag up to four or five times.
03:55So let's do that. We'll create a static constant.
03:58So I'm going to create here a static constant. It's going to be a private, well, it doesn't even have to be a static.
04:06So we're going to create a private const value and that private will be wags per play.
04:17So wags per play, which is going to be an integer, let's set it to be five.
04:21And we're going to set your private variable current wags, which is going to be an integer as well.
04:28And we're going to set that to five. Now, the idea itself is this. If our total wags reached five, then we're going to really stop.
04:37If our total wags did not reach five, we will add one to our current wag and then not do anything else.
04:46So we're going to ask here if our wags per play, which always equals five, equals to our current wags.
04:54That is such a hard word to say, current wags, current wags.
04:58So if they're equal, then I want to stop. No need to trace anymore.
05:02If they're not equal, what do I want to do? I want to add one to them.
05:07Now, just to make sure that I'm not going to have any mistakes here, I'm going to make it bigger or equal to wag tails.
05:12And then I'm going to go to wag tails and always add one to our element if it's not equal to our scenario.
05:20Now, same for play. Now, this time around, I'm not going to call it this.play. I'm just going to call it play.
05:26Now, the reason why I'm doing that is because I want to override the play function as well.
05:30And to override the play function, what I want to do before I start playing something, I want to make sure my current wags is set to zero.
05:38And that way, every time the stop is actually called inside of our timeline, it's going to add one, add one, and it's not really going to stop.
05:46Now, again, this is not strongly encouraged. I would even say it's not encouraged at all to do this.
05:51But it's a really great example of working with override even for functions that are not ours, that we can't even see what's inside of them.
05:59So I'm going to call it override function play.
06:02Now, by the way, when I took off the this, I didn't have to take it out. The this is completely legitimate.
06:10I just didn't have to. And because I didn't have to, then I removed it.
06:13Now, in my play, I again want to go to my super and make sure that the super plays no matter what.
06:18But before I ask for it to play, I'm going to ask for my current wags to be equal to zero.
06:24And if I run my application one more time, and if I click on my dog this time around, you'll see that he's going to wag his tail.
06:32But we have a problem, right? Houston, we have a problem.
06:35So let's try to figure out what's going on and why it's not actually working.
06:39So first of all, for our play, I want to trace here our current wags each time we reach reach our stop to see what's actually going on.
06:48So let's see. So whenever I click on the dog the first time, it's at zero.
06:55All right. So is five greater or equal to zero? It is. So it stops.
07:01So our problem is that we put our current wags in the wrong place.
07:05We actually want to do it the exact opposite. As long as our element is larger than our item, then we want to add one.
07:14If it's equal or greater than we want to stop, really. So let's give it a try one more time.
07:19Here we go. One, two, three, four, five. And there we go. And it works.
07:26Now, we have only one problem is when we run our application, we'll see that it runs through a loop one time.
07:31And to fix that issue of it running in the loop for one time, we want to make sure that even when the first time that it's being called, that it would actually stop.
07:43So I'm going to put here a little trace here to make to see when it stops. So that's one.
07:48OK, so here is when we stop here, we stop. And I'll put here another trace in line twenty five.
07:54And I'm going to type here still running.
07:58And I want to see if this if statement in the first time around is ending up to be still running.
08:04So we could see that still running absolutely happens once. And we need to figure out why that's happening.
08:10And probably the easiest way to check this is to just copy our variables.
08:15It might be a very simple problem that I'm just not noticing.
08:19But probably the easiest thing to do is just to trace. And we'll be able to see how many wags are per play and how many are we in our current wags.
08:26And based on that, decide what we're doing. So we could see that Max is five. We're at six right now.
08:33But it actually tells us that our equal here is our problem. Because we want it to be...
08:40And by the way, the reason why I know that is because I know that I'm adding one here.
08:44Which actually is telling me that when we're coming in, it's five and five.
08:48And if it's equal to five, which is what is going on right here, then it's going to continue.
08:54So I'm going to get rid of that equal now. And we could get rid of these traces now that we figured out what our problem was.
08:59And just give it to run one more time. And we'll see that now it's not going to animate in the beginning.
09:04And if we click on our dog once, he's going to start barking. Perfection.
09:09So we're done upgrading our dog using another tip in inheritance where we overrode a built-in feature of Flash itself.
09:19We overrode the play and stop functionalities for all of our dogs. And we could have done that in any other place.
09:26And again, it's probably not a great idea to do stuff like that inside of the stop function.
09:30Mainly because it could look very confusing to other people when they're looking on it.
09:35But in our scenario, because we really wanted to showcase and sample this, then it's fine.

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!