Creating a BaseClass
In this video we move things around taking our code out of the main class and moving our timer logic into it so we can then reuse it and work with it again. This is another OOP concept.
Video Transcript
00:00So we have a movie clip, and that's fine, but what if we wanted to have more than one
00:05timer, let's say we wanted to have five timers at the same time, or more realistically, what
00:10if we wanted to have the hours in the world and have 20 clocks running at the same time?
00:16How would we do that?
00:17Right?
00:18So the next step, I want to create another class on top of our class that will just be
00:22in charge of managing our visual object.
00:24And what it actually is going to do, it's going to be very, very similar to the way
00:29that we're working with our main.
00:31Our main is a type of movie clip, and our new class is going to be a type of movie clip
00:35as well.
00:36So I'm going to call it timeout display.
00:38So I'm going to create a new file, and it's going to be an action script 3 class.
00:45And this time around, I am going to give it its name.
00:46So I'm going to call it timeout display, and it will automatically create for me some
00:55of those functions that we're building.
00:58And what I want to do is just do a few modifications.
01:01First of all, we know that our class is going to be inside of our com, everything, fla dot
01:06time.
01:09What else do we know?
01:10We know that we want it to be a visual display object.
01:13So I'm going to use the sprite, because I might want it to be interactive, right?
01:17So I'm going to call it extend sprite.
01:20And I have to make sure that I'm actually importing import flash display sprite, right?
01:29So I'm importing the flash display sprite, making sure I got my sprite in.
01:33And I think at this stage, I could save it out.
01:36So I'm just going to click on Save.
01:38And I'm just going to make sure that I'm going into my project file, and I'm going
01:41inside of my AS3 folder, and all the way until I reach the time folder where I'm
01:47going to save the file.
01:50So far, so good.
01:51The next step I want to do is I want to connect it between my new class and my library
01:56asset.
01:58And what I'm going to do is I'm just going to copy the full path, because I need the
02:01full package and the class name.
02:04So I'm just going to copy and paste it together, so I don't have to type it in
02:09our editor.
02:10And we're just going to grab this whole piece and cut it out.
02:14And if we go into our main again, and again into our library, and in our library,
02:19what I want to do is I want to right click again into my MC timeout.
02:24And this time around, I'm not going to change my class, because what I want to
02:27do is I want to reuse the same timeout display a few places, which is basically
02:32connecting to what we're talking about inheritance.
02:34All right?
02:35So what I want to do is I want to create two different timeouts.
02:39One is going to be our LMC timeout, and the other one will be another one,
02:42which we'll work on later.
02:44So what I'm going to do is I'm going to change the base class.
02:46Right now, our base class by default is movie clip.
02:48But what if I change my base class from movie clip to our timeout display?
02:53I'm going to click on my V just to make sure that it works, and it's telling
02:56me that everything is good.
02:58And I'm going to click on OK, and I click on OK, OK.
03:02So if I save this out, and I go to my timeout display, and I'm just going to
03:06put here a little trace just to see that, is it going to be called?
03:09Is it actually referencing it?
03:12I'm just going to type here, just eight, just randomly, and run my application.
03:16We're going to see that, yep, it is definitely calling it.
03:18Fantastic.
03:19So now that we know that it's calling it, if we go back to our app for a second,
03:23we remember that we just, well, we don't have to, we could go right into that
03:27movie clip.
03:28And in our movie clip, you're going to see that we have our text field inside of it.
03:31So how about we take that definition of our text field and just put it directly
03:35into our timeout display.
03:37Now, again, we know it's public, same way that we would treat it when we were
03:41working with our main app.
03:42So we have a public var, which was ttime, ttime, it was a text field.
03:49That's all we really needed to do to gain access to our text field.
03:52So now if I just trace out ttime inside of our timeout display,
03:57I should be getting my text field.
03:59All right, the exact same way as we did with our main constructor.
04:02Now, the next step that I want to do is I want to grab all the logic that I
04:05have inside of my main app.
04:07So if we just open up that screen again, I want to capture almost everything here
04:12and just move it to the other side.
04:13So I'm just going to copy all of this, everything that is inside of my app.
04:18I'm going to copy it, and we're going to change just a couple of small stuff.
04:21All right, so if we go back into our display, I'm just going to paste the
04:25right here, and then we're going to grab a few stuff.
04:27First of all, we don't have the public app, right?
04:29Do we want to create a new LMC timeout?
04:31No, because we're right inside of it.
04:32That would be illegal, because it would create like an endless loop, wouldn't it?
04:37We don't want to add it to the stage, so all this is not really relevant.
04:41So we do want to create a new timeout.
04:43We want to call the time change, but we want to add these events.
04:46So I'm going to grab all these babies and throw them right into our constructor,
04:53and we can now get rid of our app function.
04:57All right, so our constructor is now doing basically everything we want,
04:59besides actually starting our definition out, right?
05:05Because we want to keep it generic,
05:07besides the point that we can't call itself directly in that way.
05:11Now, we're missing a few stuff.
05:12One of them, we created a new timeout,
05:14but we didn't actually grab that file.
05:16So we're going to go to that private TO, cut that out, paste it in.
05:24And last but not least, we want to grab our imports,
05:27which are basically, we imported our timeout, we imported our event,
05:31which I'm going to copy that as well, and paste it right into our import lines.
05:41Now, one thing that we'll notice that we don't, if we look at our import,
05:45we're trying to import something from the class, from the package,
05:48column, everything, affiliate time, timeout.
05:50But we're in that class already.
05:52The beauty here is because both files are in the same class,
05:54we don't have to import it anymore.
05:56So I could delete that import because our timeout will be visible to our time
06:00because it's inside of the same folder, okay?
06:03So I could delete that line.
06:06All right, so everything here should be working,
06:09but we can't really run it until we go to our app and actually start it up.
06:13So if I go to my app, what we want to do is first,
06:15let's get rid of everything that's not valuable anymore.
06:18First of all, we're not doing the time inside of here anymore, right?
06:21We move that to our timeout display,
06:24and we're not calling these two functions anymore.
06:26We're doing that inside of our display as well.
06:29And that's about it, right?
06:30We don't need the timeout anymore, again,
06:32because that's all happening behind the hood.
06:35And we're not using any events right now,
06:36so we could delete that as well.
06:38And look how beautiful our main app now became, it's much smaller.
06:43And it has less things to worry about.
06:45One thing that we could do, if we wanted to,
06:48let's say we wanted to add, first of all,
06:50we're going to run it and see that it works, first of all.
06:53So first of all, we have here a few errors,
06:55so let's try to figure them out and fix them.
06:57So first it's saying access to undefined property, mctimeout.
07:03Great, so here we go, this is one problem, right?
07:06So let's fix that problem.
07:08First of all, we don't have to call mctimeout anymore
07:10because we're in it already, or whatever it's going to be called,
07:13it doesn't really matter, but it has,
07:15we're inside of the timeout display,
07:17we're going back to our main,
07:18we're inside of our timeout display.
07:20And in that display, we have direct access
07:22to that text field, so we don't need to,
07:24and we're not allowed to call our timeout.
07:27Mctimeout doesn't exist when we're inside
07:29because we moved our calls from an external call
07:33right into our object.
07:35So now that we're in our object,
07:36we don't need those references anymore.
07:38All right, so let's give it one more run,
07:41and we see that everything is working,
07:43and that is sweet, isn't it, right?
