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

Extending Classes

Enough background lets get a class to extend another class. what to know what that means? join us and lets explore and create a few custom classes that are based off the Animal class.

Video Transcript

00:00So far, our animal was just an object. It had no ancestors, and thus it couldn't be a display object,
00:07but we actually want our animal to be associated with these animals in a few moments.
00:12And to make that happen, we actually need to convert that animal and turn it into be an interactive object.
00:18The most primitive display object that we have that could actually work for us,
00:22because we're not going to work with a movie clip yet, we'll start with a sprite,
00:26because a sprite enables us to have both click events, and it doesn't have a timeline.
00:32So a sprite is perfect for us. To make our animal actually an ancestor of a sprite,
00:38or a dependent on that sprite, or in other words, instead of confusing words and throwing just random words,
00:44what we want is actually to take all the stuff that a sprite has and just add in animal features into it,
00:50which are these properties that we just have there.
00:52To do that, let's first import that sprite.
00:55import, flash.display, not debugger, display, sprite, fantastic.
01:06So we're importing it, and the next line that we have to do,
01:09I just want to show you how we worked also with our main in the exact same way.
01:13So all this time we've been working with our public class main,
01:16and we had to say that it extends the movie clip.
01:19Why? Because our main is this, it's our root, it's everything that's on the display, and that's a visual element.
01:25And thus we had to borrow, our information here is not the whole application,
01:29we had to borrow all the logic from the movie clip and append to it and add into it all these extra goodies.
01:36So in this case, our main class, the movie clip is its nearest ancestor because it extends the movie clip.
01:43And same in our animal case, we want to take our sprite and we want to make our animal extend the sprite,
01:48so all these new features that we're adding will be added on to the sprite whenever we're inside of an animal.
01:53So let's do that, let's extend the sprite, and now our animal is a type of sprite.
02:01Okay, so our animal is a type of sprite, and that's good news.
02:05I wanted to say that this cat is an animal now, that's still true, right?
02:09It's still a type of animal, right? It's still an animal, there's nothing wrong with that.
02:13But our cat, not only is it a type of animal, it's also a type of sprite.
02:17Just as saying that a cat is a type of animal, it's also a type of sprite now, because it extends a sprite.
02:26To prove that, I could say now, cat prototype it as a sprite.
02:32And that basically says that take this animal and treat it as if it's a sprite.
02:37Why? Because it is a sprite, it's only a little bit more than a sprite, it's a sprite that's also an animal.
02:43But in our case, let me just comment down all these extra goodies that we have here before,
02:47just to show you that we're not getting any compiling errors, or not real compile errors,
02:52but we're getting here more of a warning.
02:55Oh, it's an error, type was not fine, or not compile time.
02:58Oh, okay, so first of all, we have an issue because we didn't import the sprite.
03:01So let's do that quickly, just to prove our point.
03:05Import, import, flash, display, sprite.
03:10So we're just doing that quickly because it doesn't even know what a sprite is, right?
03:13Because we don't have that in our main class.
03:15Let's run it one more time.
03:17And here we go, and we're not getting any errors.
03:19We're not getting any errors, and the reason is because our animal is a type of sprite as well.
03:24With that in mind, it's time for us to go into our inheritance line.
03:29And so I'll show you the first step of inheritance,
03:31where what we want to do is we just want to take all these animals,
03:34and we want to set them as movie clips,
03:36and automatically tell them all these animals are a type of animal.
03:39Just as much as we said that an animal is a type of sprite.
03:43On the other hand, a sprite is not a type of animal.
03:46So if I tried to do the exact opposite, if I would say here that a cat is an animal,
03:51so far so good, but if I would say that new sprite on this one,
03:55if I would say new sprite, and I would try to put it inside of an animal,
03:59well, that's not valid.
04:00It would tell me the value of static type, yada, yada, yada,
04:03basically telling me that a sprite is not a type of animal.
04:07On the other way around, an animal is definitely a type of sprite, okay?
04:10So let's take one more step forward.
04:12So, okay, we're done with that sample.
04:14Now, instead of creating it here, we're going to use our movie clips,
04:17our elements here that are on the stage already, convert them into a movie clip.
04:21So let's do that.
04:22We're going to take this dog, modify, convert symbol,
04:25and we're going to export it to action script.
04:27First of all, we're going to call it mcdog,
04:29and we're going to export it to action script.
04:31And this time around, we want to tell it that it's a type of animal.
04:34I don't remember the animal by heart, so I'm going to click okay.
04:37It's going to tell me that it can't find the definition.
04:39What definition can it find?
04:41It can't find a class called mcdog, and that's fine,
04:43because we're trying to create a linkage,
04:45as we've done before in earlier videos in the essential action script.
04:50So instead of that, I'm going to still,
04:53I just want to grab this whole package name,
04:56because I need the whole qualified name,
04:58the package name dot the class itself.
05:01We have here the nice full qualified name.
05:03I'm going to cut that, copy that,
05:05so we could paste it into our mcdog, right click properties.
05:08Now first of all, I could go to my class and say directly,
05:11it is this whole package, it is this class,
05:14making sure that yep, it could find it, it's animals, we're good.
05:18Now our base class isn't movie clip, right?
05:20That's not correct.
05:22So our base class is actually a sprite, right,
05:24because we've seen that.
05:26We're extending a sprite, we're not extending a movie clip.
05:30All right, so that works out good for us.
05:32We're going to click okay.
05:34And we're just going to check it out and see if this works.
05:37And perfect, it's working, we don't have any issues so far.
05:40Now we can't really see any difference,
05:42our dog is not really doing anything.
05:44So let's make our dog do something,
05:46so we could actually see it working.
05:48So back into our animal class,
05:50I just want to add into the constructor,
05:52instead of putting that star there,
05:54let's add some interactiveness.
05:56So we're going to add a new event,
05:58and this time around we're going to listen to a mouse event,
06:05dot click, and when that happens,
06:08we're going to call the on click function.
06:12Now, I'm not worrying about importing the mouse event,
06:15because when we create that mouse event right down here,
06:19and I'm going to call it a private function,
06:21because I don't want anyone accessing it but me,
06:23on click.
06:24And I know that in Flash These 5.5,
06:26when I actually add the event itself,
06:28mouse event is going to automatically import the line that I need,
06:32so I'm not worrying about the import.
06:34All right, so that's good for me.
06:37Now, the next step for me is I want to actually do something
06:40when a click happens.
06:41So let's on click, let's have the dog react.
06:43So I'm going to call the react function.
06:46And let's check this out.
06:48So if we run application one more time,
06:50when we click on our dog, it should now react
06:52and say that it's happy to see us.
06:54Perfect.
06:55Now let's just fix one more thing
06:57before we're done with this dog,
06:58and let's make sure that our dog actually has a mouse cursor,
07:03that our mouse cursor changes when our dog is in the stage.
07:06So I'm going to go right back up here
07:08after I added the event listener,
07:10and I'm going to make sure that mouse,
07:13or that button mode, is set to true.
07:16Now, you might be thinking,
07:17how do I remember this property,
07:19and how do I know that property actually exists?
07:21And that's a fair and perfect question.
07:23So first of all, there's quite a lot of properties
07:25for the sprite class and its ancestors.
07:28Just as right now, we're taking our animal class
07:30that's extending the sprite.
07:33Thus, we're actually creating an inheritance line.
07:36We're actually adding stuff to the sprite.
07:39We could leverage all those functionalities of the sprite,
07:41as we said before.
07:42So if we go into, and in Google S3,
07:44I'm going to search for sprite.
07:46And my first value is going to be
07:48the ActionScript 3 documentation.
07:50And we can see that inheritance line already in our sprite.
07:52So first of all, the sprite inherits
07:54from the display object container,
07:56and the display object container
07:58inherits from the interactive object,
08:00which that inherits from the display object,
08:02and that inherits from the event dispatcher,
08:05and that from the object.
08:07And all these stuff together gathered
08:09all these wonderful properties and features
08:11that created this interactive richness.
08:14And you can see even, for example,
08:16the select all was implemented actually
08:18in the interactive object and not in the sprite directly.
08:22With that said, if we didn't put here an extend,
08:25by default, there is always an extend.
08:28So if we didn't put here extends,
08:29it would automatically,
08:30hiddenly add extends object.
08:32So if we just create anything here
08:34without putting extends, it will extend anyways,
08:37only it will extend directly the object.
08:39It would go back to that basic, basic type,
08:42which is the heart of all applications,
08:44the start of everything,
08:45the few functions that are in that thing,
08:47are the base for all other classes
08:49that will be created later on.
08:51All right, so we know our animal extends a sprite
08:53and we set our button bone to be true,
08:55and we can see now that our dog is interactive and clickable.
09:00Now, our only problem now is that we want to implement
09:02the same thing also for our cat and our bunny,
09:05but we already took over that name animal, right?
09:08So if we go into our properties,
09:10we already set that our main class for our dog
09:13is our class.
09:15And notice how the base class is irrelevant anymore,
09:17because this is the real class.
09:19It doesn't need that, so it actually deleted it
09:21after we clicked okay.
09:23But if we copy this and we try to go to another animal,
09:26let's create now our cat.
09:28Modify, convert to symbol.
09:30I'm going to call it mccat.
09:32Make sure that it's a movie clip.
09:34Export to action script.
09:35And I'm going to try to set its class to be
09:38the animal class as well and click okay.
09:40And when I do that, it's going to tell me,
09:42whoa, one second here.
09:43Well, that's fine.
09:46Let's click okay.
09:47Oh, first of all, I had a typo here,
09:49so that's why it was giving me that,
09:51because I actually didn't type the whole com folder.
09:54That's why it's telling me that it's creating something new.
09:57All right, so we're going to click okay one more time.
09:59And now it's going to tell us,
10:01please enter a unique class name that is not associated
10:03with other library symbols.
10:05All right, so we have your problems.
10:07Let's fix our problem.
10:09Because our dog class is already using,
10:11and if we go into our properties,
10:13our dog class is already using that animal class,
10:15we can't reuse it again.
10:17To fix that issue, I could copy this class name
10:20and use it as its base class.
10:22And basically make our MC dog,
10:24make our dog extend the animal class
10:26instead of be the animal class.
10:28Now, if the dog is not the animal class,
10:30I have to give it a name.
10:31I have to give it a class.
10:32Now, I don't want to create a unique class
10:34just for the dog in this case.
10:35And because I don't want to create a unique class
10:37for the dog, I could type here just anything,
10:41and that anything would be then a dynamic class.
10:43But I like calling anything I create, starting with L.
10:47So I'm going to call it L and underscore dog,
10:50making me know and recognize that this is a linkage of a dog.
10:54All right, so we have an L dog,
10:56which is a linkage if I'm going to click OK.
10:59And I'm going to be okay with that
11:01because L dog doesn't exist,
11:02and it's telling me that it doesn't exist,
11:04and that's fine.
11:05I know it doesn't exist, and that works for me.
11:07And I'm going to click OK.
11:09And now that my L dog has been created,
11:11I'm just going to run the app one more time
11:13just to show you that it's still working.
11:14My dog is still clickable.
11:16It's still happy to see me.
11:17My cat is not because it can't find it yet, right?
11:20So let's go into our cat
11:22and fix our issues with the cat as well.
11:24So first of all, instead of that,
11:25we could have our base class to be our class,
11:27our animal class with the whole import line,
11:30our whole package library, the com,
11:32that everything if they open, so on and so forth.
11:35And now our main class, again,
11:36I'm going to give it a class of linkage L cat,
11:39making sure that it's going to tell me again
11:41that I'm creating a class that doesn't exist,
11:43and that's fine because our class itself
11:45doesn't really exist.
11:47And now we have two dynamically created dog class
11:50and cat class that both extend the animal.
11:53And now that both of them extend the animal,
11:55we can now see that both the dog will be clickable
11:58and the cat will be clickable.
12:00And our last hero is our bunny,
12:02and we want to make our bunny clickable as well.
12:04And as easy as that is, let's just do that really,
12:07really quickly just for practice or right click
12:09into our properties of our MC logo, our bunny.
12:13And we're going to go and export for action script.
12:16And I'm just going to change the base class to be the animal,
12:19and I'm going to call it LMC animal.
12:21And we can click OK, and we're fine with that.
12:25And we are ready to run our application.
12:28And it was that easy to add all this functionality,
12:30all this exact same functionality to all our animals
12:33using inheritance.

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!