Don't Be An Animal
In this part of the course we will create the Animal class that is going to be used to represent all animals. By the end of this video we will create 2 properties and 2 methods for our Animal class. Our animal class will be our base class to be used for all animals.
Video Transcript
00:00All right, so we know about our commonalities.
00:02The next step for us is actually to create that animal class
00:05that hopefully we'll be able to represent all animals for now.
00:08So to be able to do that, first I'm
00:09just going to prop my main class, which I just
00:12want to show it to you guys before we continue.
00:14So we have our main class.
00:15It's our basic one.
00:16It's empty.
00:16There's nothing in it yet.
00:17And our plan in the beginning is to add our non-visual animal
00:22class into our main and create a few different animals,
00:25a cat, a dog, a rabbit, using our animal class.
00:28All right, so to do that, we first
00:29have to actually create that animal class.
00:32And to do that, I'm just going to do the same old file, new,
00:35select the ActionScript 3 class.
00:38And I'm going to type inside animal,
00:39even though we're actually going to move it.
00:42So we're not going to save what comes out of it.
00:44So it doesn't really matter what you type in here.
00:46So we could actually delete everything here,
00:47because we want to do it ourselves,
00:49just until we get used to creating classes on our own.
00:52Now before I start actually typing inside the package name,
00:55I want to make sure that I'm putting
00:56this class in a separate package.
00:59All right, so we're in our folder.
01:01And in our folder, I actually want
01:02to create a separate package.
01:05And I want to make sure first that I'm
01:06starting with my domain.
01:07So like we said, we want to create our packages all
01:11lowercase, just so it's easy for us
01:12to recognize between our packages,
01:15and packages and classes.
01:17So for now, I want to put it into my com folder, everything
01:19FLA, and because this is part of the object oriented class,
01:23so I'm going to put it into a subfolder of op.
01:26And one last subfolder will be this specific class, which
01:29this class is all about inheritance.
01:33All right, so we have here a nice long chain, a nice long
01:36package.
01:37Now when you're working on your own projects,
01:39you don't have to name such long packages.
01:41But it's useful to have unique packages,
01:44especially if you ever plan on mixing files up together,
01:47which you're going to see that you do that quite often
01:49after you start getting used to building your own classes.
01:52Because this animal class, in theory,
01:54if you ever need an animal again, you could reuse it.
01:57So you want to figure out a strategy that
01:58actually is going to be really, really good for you.
02:01Currently, this strategy is really just for a tutorial,
02:03so it's easy for you guys to find it.
02:05Not necessarily, when you're in a real project,
02:07you would probably break things down
02:10in a more organized way that would help you find things
02:12by topic, maybe.
02:13But you have complete control over how
02:15you want to organize your packages.
02:18A good tip on my end, even though we're just
02:20starting with our first classes,
02:21is try to avoid putting more than 10 classes
02:24inside of a package, because that's
02:26going to help keep things organized.
02:28So back into our class, though, hasn't been created yet.
02:31We already know where our package is going to be,
02:33so let's start typing it out.
02:36So we know our package is going to be inside of a com.everythingfla.op.inheritance.
02:43And that's our package name.
02:44So open curly brace, close curly brace,
02:47and we're ready to start building our class.
02:49We have only one class per file currently,
02:52but still, you could create more than one class,
02:56which we're not going to get into that.
02:57But that's why it's important to start every class with the word public,
03:00because only one class could be public per file in ActionScript 3.
03:04So we want to make sure that the class that we're creating
03:07is our public class, so public class.
03:09And the name of our class we decided will be animal.
03:12So we're going to create our new class, open a curly bracket,
03:15close a curly bracket, and we're missing only one last thing
03:18before we could say we finished building our base animal class.
03:21And that is to actually create our constructor.
03:24And as we said before, our constructor holds all the information
03:27that's created as soon as our new animal is created.
03:30And we're going to see how exactly that's done in just a moment now.
03:33So I'm just going to type here public function,
03:36and the same exact name using that uppercase
03:38to make sure that we know that it's a class.
03:41So that's our way that we recognize a class.
03:43And we open and close our round brackets, then a curly bracket,
03:47and close a curly bracket.
03:49And we're all ready.
03:50We created our most basic, basic class.
03:52All that's left for us is actually to save it.
03:54So I'm just going to click quickly on Command S.
03:56And I am going to drive all the way through into our folder
04:00that we've just created into that package.
04:02And I'm going to save my file, making sure
04:04that we're keeping it all in the same exact name, both my file
04:07name, save as, both my class name, and my function,
04:11my constructor, all have to have the exact same name.
04:14And traditionally, we start it with an uppercase
04:16to make it easier for us to know that it's actually
04:19a class.
04:20All right, so we save that we have our file.
04:22All that's left for us is to actually test it out and see
04:24that it's actually working.
04:27Now before we do anything else in this file,
04:29I want to actually visit back into our main
04:32and actually see how we compare between creating our animal
04:37and creating our object just one more time
04:39before we start digging deep into actually building
04:41our class.
04:42So I'm going to jump into my main.
04:44And in my main file, I want to just create in the constructor.
04:46We're not going to save anything right now.
04:48I just want to show you how similar or more accurately
04:50how exactly the same thing our classes and classes that
04:54are built in are.
04:55So for example, when we worked with the object
04:57in the developer basics, we created a new object.
05:00We just typed new object.
05:02Objects, though, when we created them, they had no packages.
05:05They sat in the main package.
05:07Contrary to that, you could see
05:08that how movie clips have their own specific package.
05:11Our class is in a specific package as well.
05:13So to use it, we have to make sure we import it.
05:16But the same exact syntax that we use for a new object
05:19would be the exact same thing if we
05:20wanted to create a new animal.
05:22So let's create a new animal.
05:23So I'm going to type our animal.
05:25And because I'm working with a newer version of Flash,
05:28if you're working with an older version of Flash,
05:30you might not get these accurate hints.
05:33If you're working with Flex, you'll
05:34probably get these hints as well.
05:36But if I just click Enter and select that animal,
05:38automatically Flash is going to import the whole line.
05:42So I don't have to type it myself.
05:44If not, you just have to remember
05:45to travel through your package name and import it manually.
05:49But we're not going to worry about that because we're
05:51working with a more modern editor, which is just a side note.
05:54Make sure that if you're not working with Flash CS5
05:57or later, then you have to make sure
05:59that you're importing it manually.
06:01And it is important to do that.
06:03So just this one time, one last time,
06:05we're going to actually do it ourselves.
06:06So I'm going to delete what I have here.
06:08And I'm just going to do the import lines myself.
06:10So I'm going to import.
06:12I'm going to select my folder.
06:15My subfolder, my package, and then my class.
06:18And I'm ready to roll.
06:20And I have my class all ready.
06:21And now we're ready to actually create it
06:23just as we were a second ago, which is type of animal.
06:27And then we create a new animal.
06:29And exactly the same way, same format.
06:32So this actually opens the door.
06:33And the more astute of you that are probably noticing
06:37is that the object itself, when all this time we
06:40said the object to object, why did we say that?
06:42It's because the object is actually a class.
06:44And only after we create a new object,
06:46that object is created.
06:48That's probably one of the most confusing words in Flash,
06:51right, or in programming, that the object class is a class.
06:55But it's object itself is not a class anymore, right?
07:00So in this scenario, this is not an object.
07:01But this animal is an object.
07:03It's also a type of object, which we'll
07:05talk about in a few minutes.
07:06But we just type new animal, new that class name.
07:10And what it does, it travels through our class,
07:13creates a copy of that blueprint,
07:16turns it into an object, an animal type object.
07:20And it reads out.
07:20It crawls this function.
07:22So whatever is inside of this function would then trigger.
07:26Now, if we just start our file right now and we save it,
07:28then we wouldn't see anything, right?
07:31And you see that nothing is coming out.
07:33So really, the next step that we wanted just to test
07:36to see that our application is working, even though we
07:38see that it's working, is we're
07:39just going to put here a little trace inside
07:41the constructor of our animal.
07:43And as soon as that constructor will be triggered,
07:45as soon as that will be called, our class will be called.
07:49As soon as that object is created,
07:51this function will be called.
07:52And when this function is called, we're
07:53going to get that trace.
07:54So if we run our application one more time,
07:57and just to even confirm it one more time even,
08:00we're just going to add here just like a line just before
08:03and just after.
08:04So you could see that that trace is happening exactly
08:07after we create a new animal.
08:10And the next step that we want to do
08:12is we actually want to start digging deeper and actually
08:14building out some functions.
08:16Let's build out some really simple functions.
08:19And usually what we want to do, and I'm just
08:21going to lean on a few of these names that we have here.
08:23So we have your eyes, legs, eat, sleep, make noise.
08:26And really, the point was to show a few properties,
08:29a few functions.
08:31So let's create a few properties, variables,
08:33and a few methods.
08:34So in the meantime right now, I'm
08:35going to create everything as public.
08:37And we're going to talk a little bit more
08:38about public and private in a few minutes.
08:40So in my animal class, I'm just
08:42going to create two variables.
08:43One will be eyes.
08:44And I know it's a silly sample, but sometimes silly
08:47makes it easier.
08:48So every animal has two eyes, and most animal have legs.
08:53And in our case, all of our animals
08:55that we're working with have four legs.
08:57So that works out really, really well.
09:00All right, so we created two variables, easy enough.
09:03Let's create out two functions, or two methods,
09:05as they're called when they're connected to an object,
09:09a class.
09:10So we're going to create a new public function.
09:12And this public function will be eat.
09:19All right, so eat will basically be
09:21a function that's called whenever
09:22we want to feed our animal.
09:26And our second function, we'll just
09:27create another simple function, and that will be react to me.
09:36All right, let's just react.
09:38All right, so react.
09:39React in a way it's kind of like the animal
09:41is reacting to our gesture, or reacting to something
09:44that we're doing.
09:44And deliberately, I'm leaving it vague,
09:46because it enables us to make sure
09:47that some animals will react differently
09:50than other animals.
09:51Currently, we have nothing inside here,
09:53so let's just type here just a simple trace, such as,
09:56I'm eating, whoop, we have your little.
10:00All right, let me just show you how to fix a sort out
10:03issue like that.
10:03So when I typed this, one of my problems
10:05was that because I opened with one of these babies
10:12and not two, what I could do is just type this character.
10:16Just by typing a bracket, just a bracket of this nature,
10:20I'm going to be able to avoid my string being finished,
10:23and then I could continue my string.
10:25Or I could have, if I wanted to, not do that,
10:28then I would have to use a double curly, double quote,
10:33sorry.
10:34All right, so I'm eating food.
10:38That works for me.
10:39I'm eating food.
10:40And for our react, we'll say, I'm happy to see you.
10:49All right, so that really worked well for us.
10:53So we have your two reactions, two functions, two animals.
10:57Well, we have one animal, but this
10:58supposable basic function in the beginning
11:00is going to be used for us to recreate
11:02the feeling of all these three different animals.
