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

Multiple Interfaces

as3 interfaces are easy and fun once you get used to them but what would you think if I told you you can use multiple interfaces at the same time. I know might sound a bit confusing but we've been through so much already that we should be just fine. In this video we continue with our fire example and now create a new interface that will be in charge of playable elements. The idea behind here is that we want an interface that would define what elements are playable(Aka can start and stop in the control of external elements). In other words what objects/classes will have public play/stop functions. By the end of this video we will add this new interface and implement it on the Dog and our new Fire. We then will add new functionality to our Main class where we will randomly animate playable content when the user is not interacting with elements. One of the main things we are learning here is the capability of one item to have more then one interface(there is no real limit to how many) contrary to inheritance that can only have one element in the inheritance line in as3.

Video Transcript

00:00Alrighty, let's move on to our next topic, which is going to be multiple interfaces.
00:04So let's create a new interface.
00:06We've just created the fire class, and as you've noticed, I created a stop function
00:10in it, which is public.
00:13What I want to do is I want to create an interface.
00:15I'm going to click on File, New, and I'm going to select Interface, and let me just
00:19grab our directory path, our package path, before I do that, we're going to create
00:27a new interface.
00:28And I'm going to call it .iplayable.
00:32And the idea behind iplayable is that it has a play and stop function.
00:36So I'm going to save this, click OK, and it's going to create my new interface.
00:41I'm going to immediately click on the Save button, File, Save, go into the right
00:46directory, make sure that I save it into our pull-if-formism folder as iplayable.
00:51That is fine.
00:53And let's start defining our method.
00:54So we really just want to have two public functions there.
00:57So we don't need even the word public.
00:59We just need function, play, open, close, round.
01:04It doesn't return anything, and move on to the next one.
01:07We don't want to use the definition like we said.
01:09So function, stop, void, and there we go.
01:14So we've just created our play, stop functionalities.
01:18All that's left for us is to decide which animals are going to be playable and
01:22which animals are not going to be, well, not only animals, which things are going
01:25to be playable and which things are not going to be playable.
01:29We're going to, even though they all have the play and stop function right now
01:32because all the different classes have them, have it now.
01:36The fire has it because we added the play, it doesn't have the stop yet.
01:40The animal has one, and the dog and cat are inherited by the animal,
01:45which is inherited by the movie clip.
01:46So really everything has it besides fire right now.
01:49But we want to treat the iplayable not as it really has a play and stop
01:53button, but it's okay to play with it or that it could play in the background
01:58without affecting it.
02:00So a cup wouldn't be true because if we let it play automatically,
02:04the cup will get empty and we don't want the cup to be empty if someone
02:07didn't click on it.
02:08On the other hand, the dog barking or our fire are ideal candidates
02:12for random animations.
02:14All right.
02:15So let's do that.
02:16So first of all, for our fire, I want to implement it, implement it
02:19to be an iplayable.
02:21So I want to show you how that works.
02:23So first of all, we see in our implements itouchable, all I have to do
02:27is put another comma and then set the next one, which is going to be
02:30iplayable.
02:31Now my fire should be touchable and playable.
02:34Now, of course, if I run it, I'm going to see that I'm going to have a problem
02:37because I do have the stop method, but I don't have the play method.
02:40So if I run my application, I should be getting an error message
02:44and it's going to tell me interface method play and namespace iplayable
02:48not implemented to the class fire.
02:51So what I want to do in my function, I'm going to create a new public function.
02:58I'm going to call it play just so I'm supportive of this play functionality.
03:03And really what I'm going to do is I'm just going to call the touch function.
03:06So behind the hood for our play function is really just going
03:10to be to be touched again.
03:12That sounds really bad.
03:13All right.
03:14So in our scenario, the fire is playable automatically,
03:19which works really, really nicely.
03:21Let's look at our cat and dog and see if we could use that functionality in them
03:26as well.
03:27So if we look into our cat itself, we'll see that we're resetting our function
03:33and we start playing.
03:35That looks good to us.
03:36For our cat, we are not implementing over again the play.
03:44AKA for our cat, it probably wouldn't be a good idea to just start playing it.
03:48And as such, what we're going to do is we're only going to set the eye
03:53playable for our fire.
03:54And we're going to set our eye playable for our dog as well.
03:58So our dog, although as an animal implements the eye touchable,
04:03we're going to implement it in the dog itself to implement eye playable.
04:10All right.
04:10So now we have a fire and we have a dog that our eye playable.
04:13They should run and let's see.
04:15Oh, first of all, we need to import it.
04:17So let's import it.
04:19Import com 0 to geek ope polyformism eye playable.
04:32Run it again and here we go.
04:34So we first, we have everything.
04:35There's nothing really, really special going on right now.
04:39But what we could do now is we could have a random animation going on.
04:44All we have to do is let's, for example, in our main class itself,
04:48let's start adding this random animation.
04:50So what I really want, what I really want happening
04:53is I'm going to create here a private function, a private variable,
04:57actually, which is going to be an array.
05:00And in that array, it's going to be a playable items, which
05:04is going to be an array.
05:09In our constructor, I'm going to, as soon as we started,
05:12I'm going to set it to be instantiate it.
05:17And I'm going to create a private function, which
05:19is going to be called add playable, add playable, which
05:24is going to add another element.
05:28And this item is going to be I playable.
05:32And notice that our I playable was imported again.
05:37And all we're going to do is we're going to push into our A playable.
05:44We're going to push our new item into our A playable array.
05:48All right, next item for us.
05:51All right, next item for us is now to actually add the item.
05:54So I'm going to add them right underneath here.
05:56And I'm going to decide, based on the elements that we said,
05:59that we want our dog and we want our fire to be playable.
06:03So I'm going to go to these elements,
06:04make sure that they have a name, because now we actually
06:07want to refer to them from the stage directly.
06:09So I'm going to call this one fire.
06:14And I'm going to call the dog, I'm going to call it dog.
06:17Very creative names.
06:19All right, so now that they have names, both the dog and the fire,
06:22I can now approach them and just push them into our add playable.
06:26So we're going to add a playable fire, add playable dog.
06:34All right, so we've added both the dog.
06:37We added the dog and we've added the fire.
06:39And all that's left is I really want
06:41to create some sort of random motion that only happens
06:43if the user is not interacting.
06:45To do that again, I personally love using the set interval.
06:50Well, I love using the set interval.
06:52So import, flash, utils, set interval, import, flash,
07:00utils, clear, interval.
07:07All right, so first of all, as soon as our application
07:10begins, sorry about that, I'm going
07:13to create a new private variable.
07:15That private variable, we'll just call it interval.
07:17And I usually call their intervals just interval
07:19as long as I have only one of them inside of my class.
07:22And if there's more than one, then I
07:24start being creative about figuring out
07:26a name for my interval, because I think
07:28it's descriptive enough when you call it an interval
07:30that there's only one inside of your class.
07:34So I'm going to set an interval and I'm
07:35going to call this auto play, the function auto play.
07:39There's going to be a delay of 10 seconds.
07:411, 2, 3, 4, 5, no, 4.
07:46All right, so every 10 seconds, we
07:47are going to randomly play something.
07:49For our example, because we don't want to wait too long,
07:52so I'm going to set it to be 2,500,
07:55and then we'll upgrade it later on.
07:57So I'm going to create a new private function somewhere here.
08:00So private function auto play.
08:09And don't worry about it setting to be blue.
08:11It just means that there's something out there,
08:12some class that uses the auto play function, probably
08:15one of the video players.
08:16But we don't have to worry about it,
08:17because our movie clip doesn't have any auto play method,
08:20so we could create one for it.
08:23All right, so our auto play, what we want to do
08:25is we want to really, what we want to do
08:26is select randomly an element.
08:28So I'm going to go to A playable items.
08:34And I'm going to basically take my math.floor, math.random,
08:46times A playable items.length.
08:51So it's a really long amount of code.
08:54So let's go through this step by step.
08:55So first of all, what I'm doing is
08:57I'm randomly selecting a value between zero and the length.
09:00And after I get that random value, what I want to do
09:03is I want to floor it, because I never
09:04want to get the highest value.
09:06And I actually want to get between zero and one
09:08less than the highest value.
09:09And this will make sure that I get that.
09:11So now that I got that element,
09:13I could now approach it directly
09:15and just set it to play.
09:18And let's see if this works.
09:20So now once every two and a half seconds,
09:22if we run application, if there's no errors,
09:24once every two and a half seconds,
09:26our dog or our fire will go.
09:27So now we see our fire is going.
09:29And another two and a half seconds,
09:30again, our fire is going.
09:31And now our dog is going.
09:33So we see that our animations are happening randomly
09:36each time, even though the user
09:37is not interacting with them.
09:39So let's add here a little bit of extra cushion time.
09:42So let's make it every five seconds.
09:46And let's do one extra thing is,
09:48basically, whenever the user clicks on something,
09:51we're gonna stop our current thing that's animating.
09:55To do that, we probably wanna reference it in our element.
09:58So I'm gonna create a private variable
10:00and I'm gonna call it current player,
10:04current play, which is gonna be an I playable.
10:10And our current play, every time we define a new one,
10:13I'm just gonna go it and set to be that.
10:16I'm gonna make sure that it's set to be that.
10:20I'm not gonna stop it and if it's going in out of play,
10:23the only time I will stop it
10:25is if the user clicked on something.
10:26So if the user clicked on something
10:29and their current play is set,
10:31I'm gonna set my current player to be stop
10:35and I'm gonna set my current play to be nothing.
10:38This way I'm making sure that I'm not gonna
10:40call this stop over and over and over
10:42and we only set it once
10:43and then I'm gonna remove it from my list.
10:45Now, last but not least, I wanna reset my timer.
10:48So if the user clicked on something,
10:50I wanna clear the interval
11:00and I wanna call the interval again and reset it.
11:03And by the way, this I probably wanna do
11:05outside of my if statement
11:07because even if nothing is set,
11:09I wanna each time that the user clicks on it
11:11to stop my interval and restart it over again.
11:14So now if I run my application once every five seconds,
11:17so one, two, three, four, five,
11:21our fire now just started.
11:23And now let's wait until our dog will start
11:25or our dog is starting, as soon as I stop the water,
11:28now my dog should have stopped
11:30but he won't stop because our stop function
11:32is kind of like overridden
11:33but if I click on the stop now
11:34while the fire is going, it will stop.
11:37Which works perfectly for us,
11:38we're not gonna change the fire again, the dog again.
11:40So let's just wait one more time,
11:42wait for the fire to start.
11:43As soon as the fire starts,
11:44I'm gonna click on the water
11:45and it immediately is gonna stop my fire,
11:47giving us again a five seconds delay
11:50that if I click anywhere that is clickable,
11:53then these elements will not be, will not animate.
11:56If I click on the place that's not clickable,
11:58then it will, excellent.
12:00So now in this part, what we actually learned
12:03is we learned that we could actually
12:05add multiple interfaces.
12:07Not only did we add multiple interfaces for our fire,
12:11and by the way, you could put as many of them as you want,
12:13we also saw that we could actually continue
12:16even though our animal itself has an implementation
12:20of an interface which is eye touchable,
12:22we could make it so that the cat itself
12:24will not have the eye playable, but the dog will.
12:27And our dog is also playable and also touchable,
12:32but our cat is only touchable.
12:33So even we could do it or all of them at the same layer,
12:36or with inheritance, we could choose
12:38a different layers and different depths
12:40and then add our interfaces according to our needs.
12:42Now, you still might see a lot of potential in interfaces,
12:46but not really see exactly how would you take it on
12:49in real projects.
12:50And the honest truth is, is you have to just try it out
12:53a couple of times before you really get
12:55and understand how powerful it is,
12:57and you don't have to use it all the time.
12:59It's definitely useful, but not mandatory
13:03in every single project.
13:04When you're building games,
13:05it becomes really, really useful
13:06because then you could create, for example,
13:08different styles, different types of enemies,
13:10or different types of elements,
13:12cluster them all together and treat them as one.
13:16So when you're outside in your main class,
13:18like in our scenario,
13:19our main class is very, very simple and very coherent
13:23because we don't need to differentiate
13:25between different things we don't care about, all right?
13:28So that's it for multiple interfaces.

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!