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

Dispatching Events

In this video we take a quick look at how to dispatch events.

Video Transcript

00:00Let's say, for example, I have in the animal class, whenever an animal is clicked on, not
00:06only I want to touch it, I also want to broadcast to the world that this animal was touched.
00:10Now I'm not going to go into the greeny details because there are so many details involved
00:15with here.
00:16I just want to show you one thing.
00:17We were talking about inheritance before.
00:19We know that our animal inherits from that movie clip class, but we know that the movie
00:24clip class inherits from a display object container, and then from a display object, and then from
00:31inter event dispatcher, and a lot of others, and you don't have to remember it by heart.
00:36You could always, as we always say, alright, so Google has three, we could type our movie
00:40clip, and we could see that beautiful path just going to the first top one, and then
00:45we could see it all the way here on the top, we could see that inheritance path that
00:48it's sprayed, blah, blah, blah, blah, all going all back.
00:51What I care about is event dispatcher.
00:54So now we've used this event dispatcher in our learning, and if you're going consistently
00:59from the developer basics, and you're just going step by step, we used events, but we
01:03didn't create our own events.
01:06I want to change that.
01:07Let's start creating our own events.
01:09So to do that, I'm just going to show a very simple sample, we're not going to delve
01:12too deep into it, but we're going to start using it a little bit and a little bit each
01:15time until this just becomes a second nature.
01:18Now I want, whenever the onclick function gets called, I want it to trigger a new event.
01:23I don't want it to call a function, I want it to say, for anything that's listening
01:26to this animal, let's say any animal, well, even better, any time the function touch
01:31gets called, I want to trigger an event that tells the world that this animal has
01:34been touched.
01:35To do that, I'm just going to create a new dispatch event, and I'm going to call,
01:40it's actually really written that way, dispatch, dispatch event, open bracket.
01:48Now I need to create a new event.
01:50Now we've seen how to create things before objects, so we could also create an event.
01:53We have to make sure that we're importing the event, but that's going to happen
01:56automatically for us, so we could create a new event and dispatch a new event.
02:00Now all it's expecting is what has happened, does it bubble, is it cancelable?
02:06We're going to ignore the bubbles, we're going to ignore the cancelable because
02:09that's our main topic, and if you want to dig deeper into events, which I
02:13obviously encourage you to do at this stage, right now we're just going to
02:16dispatch an event, so by default it's not going to bubble.
02:20But you know what, well, let's leave it that way.
02:22For default, it's not going to bubble, which means that we can't assume, if it
02:25doesn't bubble, we can't assume that if we're listening to the anims function,
02:29that it's going to know what's going on.
02:31So let's create here that event, go back to the animals, and I'm just going to
02:34quickly continue, so I'm just going to give it a name, and we learned already
02:38about static variables, have we?
02:41No, we haven't, so let me not say things that we haven't learned yet,
02:44we'll learn about that in a minute.
02:45All right, so we're going to create a new event, and I'm going to call this,
02:47I'm just going to put a string inside and just give it a name.
02:50I'm going to call this event touch.
02:52All right, close that bracket, close that bracket, we've got a nice dispatch event.
02:56Everything should be working the same, nothing will be noticeable at this stage,
03:01things are clickable, but we don't really care.
03:04Now the next step is now that we dispatch it, that's all I have to do.
03:06From now on, anytime line 32 happens, it's going to tell the world,
03:11touch happened, a new event happened, which was a touch.
03:14Now let's listen to it, so we know how to listen.
03:16Now we created our custom event.
03:18All right, and this custom event belongs to the animal class.
03:21And we know that it doesn't bubble, which means that only the animal class itself
03:25could actually trigger this event.
03:28So we have to listen directly to an animal class,
03:30we're saying that is an animal class.
03:32In other words, we have to listen to a dog, a cat, a rabbit,
03:35anything that is an animal.
03:36All right, so let's do that.
03:38So let's listen to the cat, cat.addEventListener.
03:44And I'm going to add event listener, and I can't find my touch, right?
03:48It doesn't exist, so I'm going to actually just put your string.
03:51All these stuff are actually strings, and we're going to look into them
03:53in just a second.
03:54So I'm just going to put here that string that I put inside of that dispatch
03:57that I put here, which was touch.
03:59So I could come here and write here touch.
04:02I'm going to ask event listener touch, and I'm going to call onTouch.
04:06And we can now create another private function here, onTouch.
04:13It's an event, it's a type of event.
04:15How do I know that the type is event?
04:17Because I used that basic event type.
04:20Which is the basic way you could use the built-in one.
04:22So I'm using a new event.
04:23So because I'm using a new event, it's going to send out an event.
04:27So I'm going to move on, and I'm just going to trace here
04:32something was touched.
04:36And to even help it a little bit more, I could say e.target and e.currentTarget.
04:43Now, just because we know that it doesn't bubble, target and current target,
04:47there's no relevance to them, because only one thing could be it.
04:51So let's run that application and see what happens.
04:54So now if I click on the cat, which is the only thing that's
04:56clickable right now, well, not clickable,
04:58the only thing that we're listening to that touch event.
05:01And if I click on my cat now, we'll see that something was touched.
05:05That cat has been touched.
05:07And as we've seen also here, it doesn't really matter.
05:10Bubble doesn't bubble.
05:11We don't have bubbling, so it is always the cat.
05:13There's no such thing as who called it and what was touched,
05:16because there's no bubbling.
05:18OK?
05:19Now, I'm not going to go into showing you the bubbling part,
05:21because you could play with it.
05:22And you could watch the two-hour tutorial
05:24on the events, which is a two-part.
05:25It's a rough one.
05:26It's an old one.
05:27We're probably going to redo some stuff in it,
05:28but it has a lot of gems in it.

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!