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

Creating a Class

Its about time, you couldn't be more ready then you are now to start working with classes. Its ok it might be a bit hard in the start but as long as you understand the basic concepts and can pass the exam of this course your doing well. The more you practice the more natural it will feel.

Video Transcript

00:00Very good. So at this point, we're starting to get to recognize what are these packages.
00:06And what I want to do next is we want to build our actually our first class that's not our main class.
00:12So we're kind of breaking away from that one class applications that we've been having so far.
00:18To do that, we actually want to create a new class. And to do that, I'm going to before I even create that class,
00:23let's explain what we're trying to do. We're trying to create a little timer,
00:26basically an application that will build a call backwards in time.
00:30If we send it a value, basically telling it, let's say when is my next birthday,
00:35or when is the next deal that you're going to have on your website.
00:39And we just want to have like a timer that's telling you another five days,
00:42and three minutes, and two minutes, and one minute, and so on and so forth.
00:46To do that, we want to create a separate class so we can reuse it and add maybe two timers at the same time.
00:51Maybe we have two events or more, and we want to place them all inside of the same file.
00:56Or we want to reuse this class, and then use it and leverage it in other applications.
01:03All right. So the way we're going to do that, I'm just going to revisit that folder
01:07where we were working before we had our AS3 folder, which we separated our source files.
01:12And in that folder, we have our com folder, our everything fla.
01:17And then inside of it, we have our app.
01:19Now, I want to create a new folder inside of here, and I'm just going to call it time.
01:24All right. And inside of that time, we're going to put inside there a timer.
01:29The next step, we actually want to create the class, right?
01:32So I'm going to create a new class, file new, and I'm going to select the ActionScript 3 class.
01:37Now, if you don't have ActionScript 3 class, if you're working on a previous version,
01:41if you're working with Eclipse, you're working on any other variation
01:44where you don't have immediate direct access to just a template, new document, AS3 class.
01:51Just create a new .as file, a new text file, and throw it into Flash, and that's totally legit.
01:57If you're working with CS3, CS4, just select from the menu, AS3.
02:00It doesn't really matter if it's a class interface,
02:03because we're going to delete everything that they put by default,
02:06even if you just select an ActionScript 3 file.
02:08As long as your extension is .as, then you're good.
02:12All right, so I'm going to select a .as class, and it's asking for a name,
02:18and just because we want to practice, I'm just going to type or anything, click enter,
02:22and I'm going to delete everything, and we're going to build it out together.
02:26So the first step that we want to do now that we have a file up and ready
02:30is we actually want to tell our class where is it going to be located.
02:35Now, we know already, if we look at our file path again,
02:38that it's going to be located inside of our com, everythingFLA, time folders.
02:43So let's tell Flash, and the way we're going to do that,
02:45we're going to use the keyword package, and we're just going to spit out the location,
02:50everythingFLA, we have our timer folder,
02:54and then we're going to open up a curly bracket,
02:56and we're going to close a curly bracket.
02:59And it's exactly the same way if I just jump for a second into our app.
03:02It's the exact same way as we placed our com.everythingFLA,
03:05because that's where we put our app class in.
03:09And we can see the very, very close similarity,
03:11which it's not only similarity, it's basically the same thing.
03:14The only difference is now that we're going into a subfolder,
03:17which is a bit deeper.
03:18Now, the next step we want to give a name to our class,
03:21and let's say we're going to call this class timeout, all right?
03:26And we want to call the class timeout, so I'm just going to type class,
03:29and then I'm going to type timeout, and I'm doing it in uppercase.
03:33And notice that it's a good convention to remember to always call your classes
03:37with an uppercase, and you start your packages and your variables with lowercase,
03:42because that makes it easier for you to notice what is a class
03:45and what's not a class.
03:47And that's all we really had to add into the line of our class.
03:50Now, contrary to our main application,
03:54which always extended a movie clip because it was a display object,
03:57and that was the basic data type that we were used to
04:01and we're working with so far,
04:03working with a basic class by default is going to extend something
04:07that's called the object.
04:09And we worked a bit with object, and everything in Flash at some point
04:13is inherited by the object.
04:15And we're going to look deeper into what inheritance actually means,
04:18but what we know so far is that every application is basically leaning,
04:22every class is leaning on a previous class,
04:25all the way up until the object.
04:28Now, even if we don't say that our application is leaning on that object,
04:32it still does.
04:34So for now, we're not going to say anything,
04:36but behind the scenes, Flash is going to take our time now
04:39and add those keywords, extends object by default,
04:43even if we don't do it.
04:45So if I open my curly bracket, close my curly bracket,
04:49and there we go, we have our class definition.
04:52If we look again into our app, we're going to see that in our app,
04:55we're going to be standing out public keyword.
04:59And that public keyword is really important.
05:02We're not going to look into private public classes at this point,
05:06but at this point, really what we want to get out of it is basically
05:09what we're saying is that this is the class that is going to be public
05:13inside of this folder, or inside of this file more accurately.
05:16So if we would place more than one class,
05:18which we're not going to do that at this point,
05:20then this is going to be the class that's going to be visible
05:23by other classes or visible to the world.
05:26So I'm going to define it as a public, a public class.
05:31And we're going to look more into depth into the details
05:34of different types of ways of defining our classes later on.
05:37But for now, all of our classes are public.
05:40We have only one class in every file,
05:43and they're always inside of our area of our package.
05:47So we have already, we've crossed through quite a lot of barriers.
05:52The last step that we have left, and we have to do that
05:55for every single class that we create,
05:57we want to create something that's called a constructor.
06:00And what is a constructor?
06:02A constructor is basically, and if we look into our app
06:06for one second, just again, compare,
06:08remember that we've been putting all of our code
06:11inside of our app, inside of our constructor,
06:14now that we gave it that name.
06:16Why did we put it in our constructor?
06:18Because by default, we said that by default,
06:20Flash is going to initiate this class.
06:23And when it initiates this class,
06:24it's going to run the code inside of that constructor.
06:27Because a constructor, as its name implies,
06:30constructs that object.
06:32So from a class, it helps convert classes
06:35into those blueprints, because this is just a blueprint.
06:37It helps convert that blueprint into reality.
06:41And basically, what you would put inside of your constructor
06:44is the code that basically describes
06:48how you create an object.
06:50There might be some things you want to do,
06:52such as making a trace before you build an object.
06:57So let's keep it simple for a second.
06:59And in our new class, we're just going to create
07:01a new function, a public function, again.
07:04We want to make it public for now,
07:06and we're going to deal with what that means
07:08in a few moments.
07:09And we want to keep our timeout,
07:11we're going to call it the same exact thing
07:13as our class, as our constructor,
07:15because that's what Flash will expect.
07:18We're going to open and close a regular around bracket,
07:23and then we're going to open a curly bracket
07:25and close a curly bracket.
07:27And there we go.
07:28We have our class.
07:29It's all ready.
07:30It's set to go.
07:31We could test it out.
07:32All we have to do is save the file.
07:34So let's say, file, save as,
07:37and we're going to go inside of our project file,
07:40and we're going to go into our as,
07:41com, everything, fla, timer,
07:43and we're going to name our file, actually,
07:45the exact same name as our timeout.
07:47Now, of course, we want to keep the exact same upper cases,
07:51everything.
07:52It's case sensitive.
07:54Now, if you're working with the build,
07:56then see this file, it's much easier,
07:58because if you gave that full class path name,
08:00and you gave all your file a structural name,
08:02then it will automatically help you
08:04and kind of cut down some of your coding time,
08:06and the same is true to Eclipse,
08:08different Eclipse environments.
08:10All right, so I'm just going to click on save as,
08:12and we just saved our file timeout,
08:15and we now created a new class,
08:17our new brand new on our own class.

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!