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

Events That Bubble

We take a revisit and look at events in connection with our new found skills of OOP.

Video Transcript

00:00Alright, so obviously the best encouragement I could give you at this stage is when you
00:04go to your code, start thinking about dynamic things.
00:07Start thinking about how could you call things less, how to call things directly less, but
00:11call them without actually saying their name.
00:14And we're going to see a lot of ways of doing that.
00:15If it's through the events, which you'll use quite a lot if you're not using them already,
00:20in your events you'll always find those hidden gems of the e.target.
00:23Now I wanted to just show one more thing.
00:25Now, even though this works for me, right, but maybe I had your hundreds of animals for
00:30me to go through every single animal, and for me to go through every single animal, and even
00:36more than that, could be that I want anything that's clicked on the stage to react in that
00:42way.
00:43Anything that could be clickable.
00:44For example, these text field legs, let me just find these text fields, animals, for
00:49example, is a text field.
00:51It's a classic text field.
00:52It's a dynamics text field.
00:54This text field in theory, I could move it around and animate it, and if I only had a way
00:57to approach it.
00:58Now, unfortunately, I didn't give it a variable name.
01:01I can't call its add event listener on its own.
01:04So let's look at what we're doing here.
01:05First of all, I'm going to disable animation just for a second, and I'm just going to
01:08create here an add event listener, and I'm just going to add event listener, mouse
01:15event.
01:16Whoopsie.
01:17I'm just going to copy this whole same block that we just had here, the onclick.
01:21Only this time around, we're going directly to our stage.
01:25We're adding this click event directly to our main.
01:27Instead of going directly to our elements, let's see what would happen.
01:30Well, obviously, we should trace something in the click, or that would be pointless.
01:34So first of all, let me just put here a little trace, and we're going to trace e.target.
01:37All right, so first of all, if I click on the dog, I'm going to see that, okay, I'm
01:43getting that dog back.
01:44I click on the cat.
01:45I'm going to get the cat, and click on the movie clip, this movie clip, which is
01:50probably an internal thing of the rabbit, an internal movie clip of the rabbit.
01:53I could click on this text field, that text field, and so on and so forth.
01:57So we could see that they're reacting, this target is reacting to our mouse.
02:01So I could even just continue those animations that I had before.
02:04Now I could throw on the stage if I want hundreds of things.
02:07As soon as I click on it, they'll be able to react, they'll be able to animate.
02:10Now, we're going to have a problem with the rabbit, we're going to try to see
02:12how to solve it.
02:13So first of all, it doesn't matter what on the stage that is clickable,
02:16that I click on it will just start dropping.
02:19Just like that, just by adding that code.
02:20So just with three lines, we've created an animation to anything we have on the
02:23stage if we wanted to.
02:25Now, obviously, sometimes you don't want to do it to everything.
02:27You want to control it.
02:28And sometimes you'd want to control the best way to control these type of stuff
02:32is throwing them into their own unique movie clip.
02:35So for example, let's say you could, or Sprite, probably more accurately.
02:38So I could have created here a private var, call it animal bucket,
02:42anim bucket, anim, well, anims, let's call it.
02:46It's going to be a Sprite.
02:49Now, all I want to do is I'm just going to create in my main, right,
02:52as soon as I start, I'm going to create a new Sprite.
02:54So I'm going to go to my animals and say that you are a new Sprite.
02:58Open, close bracket.
02:59Now I have an animal Sprite.
03:01I want to add it to the stage.
03:03I'm going to add child, anim, it's empty.
03:05No one could see it at all.
03:06It's XY position is the exact same XY position as my main.
03:10So it's exactly in the top right when I create it.
03:12It's empty though.
03:13What I want to do is I want to throw all my animals inside of it.
03:16So only they will be clickable.
03:18The easiest way to do it, first of all, if I wanted to type this just shortly,
03:22I could have done the exact same thing, only throw in this new Sprite.
03:26We'll do this in a second, because I actually
03:28wanted to talk about that separately.
03:31So we created our ads child.
03:33We have a new Sprite on the stage.
03:34We can now take our animals.
03:37I could just go to my anims and just go one by one
03:39and add into the, oops, what am I doing?
03:41Add the animals that I want to add into.
03:43So first of all, I want to take the animals, add child,
03:46and I want to add the cat into it.
03:48As soon as I add the cat into it, it gets removed from where it is.
03:52And it's just dumped right into animals.
03:55So I could do that also for the dog class.
03:57I could do that for the rabbit class.
03:58And just to prove that I could go to my animals class
04:01and I could set it to be visible false, or even better, we'll do an alpha.
04:06So it's very clear to see it.
04:08It's there.
04:09Alpha.5, we'll see that all of our things that are inside of that class
04:12now are then dimmed down.
04:14Now, we didn't want to do it to dimmed down.
04:16We wanted to control them.
04:17We only wanted them to be clickable.
04:20So now I could approach my animals class,
04:22and I could just say, just this one.
04:24I want to listen to click events on it.
04:26By doing that now, I controlled.
04:28I could say, oh, these are going to have one reaction.
04:31These are going to have another reaction.
04:32But at the same time, these don't have any reaction now at all, right?
04:35Now, only problem I have right now, which is not a problem,
04:39is sometimes, for example, I'm just taking even one step further.
04:44So sometimes I'm going to take this animals,
04:46and I want them to animate all together.
04:49Maybe I just wanted to have an event listener,
04:50and I wanted to move all the stuff that
04:52are inside of the animals class all together.
04:55Now, my problem is, when I'm doing this target,
04:58what's happening?
04:59When I'm clicking this target, I'm
05:01going to ask you the question, even though we
05:02haven't really learned it.
05:03We do have a two-hour video about events,
05:05but I thought it's a good idea to have another topic about events
05:08just to get it connected to object-oriented programming
05:11because it makes sense to connect these dots together.
05:15When I'm clicking on the dog, and I'm
05:16going to ask you guys that question in a minute,
05:18even though we didn't really, really learn about it,
05:20unless you watched the two hours about events.
05:22If I click on the dog, the dog is moving,
05:24but am I really listening to the dog?
05:26No, I'm listening.
05:27If we look back, we're listening to the animals.
05:29Why aren't all the animals moving?
05:32Why is my target that dog?
05:34So I'm going to throw that back at you guys,
05:36and I'm going to see if you guys could figure that answer.
05:39So let's randomly select someone.
05:44No, I don't want to answer it, so let's do it again.
05:47I don't know the answer.
05:48I was hoping one of you guys knows.
05:50No, we asked you enough, Carol, right?
05:52It's not fair to ask you every two things.
05:55Uh-oh, but Ramaz, you think he could join us?
05:58Answer?
05:59How about we give it a second?
06:01We'll see if he's still here, and if not,
06:03we'll let Adam answer.
06:05Yay, Ramaz, great.
06:06So this is going to be a bit of a guess,
06:08so don't worry, none of us talked about this yet.
06:11So I'll repeat the question in the meantime.
06:14All right, so we're back to the questions.
06:16We have our animals class.
06:17We've just created a new spray.
06:18We added the cat-dog rabbit into it.
06:20We're listening to the animals class,
06:23but when we click and we run it, we click and we run it,
06:27and we click on the dog, why is only the dog moving?
06:31Why isn't everything moving?
06:33It's a very hard question, and don't worry,
06:34but just give it a guess, see if it works.
06:37Yeah, yeah, he's the lucky guy that got the hard question.
06:45Yeah, yeah, I feel the same way.
06:51That's a great way to break in.
06:54Well, that's a good answer.
06:55That's the option that got clicked on, right?
06:58But at the same time, I could say,
07:00but I really want all three to get clicked on,
07:04but that's a good answer.
07:06The answer is, yeah, it's true that there's a bucket,
07:09and in that bucket, we have here this imaginary bucket
07:13that has all these stuff in it,
07:14but we actually clicked on that dog,
07:16and now that answers the question of what is target?
07:19Target is not the thing that started the event,
07:22because we could see that here is where I'm requesting
07:24the event, I'm asking it here.
07:26Now, without going too deep into events,
07:29because events, you know, it's a whole two hour topic.
07:32Events bubble, events bubble would display events bubble.
07:35That's the way they go from one object to,
07:39it basically goes in this hierarchy of from the parent,
07:45so this is the parent, this is the stage,
07:46and this is my new class that we created,
07:49we called animals, and only here is the dog, right?
07:51So this is my stage, this is my class, this is the dog.
07:53Whenever I click on the stage,
07:56it first checks to see if there's something
07:58underneath there to determine what was clicked on.
08:02It finds this thing that's stacked highest,
08:04the thing that's closest to the user.
08:06Whatever's closest to the user is what was clicked on.
08:09So whatever was closest to the user is the dog.
08:11Now, it has to broadcast this message,
08:13and let everyone know, whoever's listening,
08:16that that dog was clicked on.
08:18Now, we go really deep into it in those two hours
08:20about events, which you can find in the site itself,
08:22but what happens here is when I'm clicking
08:25on the stage itself, the flash basically is searching
08:28what it needs to be clicked on, it bubbles through,
08:30it finds it, well, it knows that the dog was clicked on.
08:33The dog then broadcasts, it starts all the way
08:36from the bottom up, actually.
08:37It tells the stage that the dog was clicked on,
08:40it then tells that class that we created,
08:43it tells the anims that it was clicked on,
08:45then it tells the dog it was clicked on,
08:47then it goes back to anims, then it goes back to the top.
08:50Now, you could even see that if we run the application,
08:52because you remember that we had a click event
08:54in the dog itself, and notice that when we click
08:56on the dog, I'm happy to see you bark, bark.
08:59First of all, let's just add one more thing to this.
09:02Let's just add a trace inside of here,
09:05so we can actually see when that click happens.
09:07So I'm gonna trace here, ie.target was clicked.
09:16All right, run it one more time.
09:17And if I run it now again, you'll see first of all,
09:20the last thing that happens is the main in this time around.
09:23So basically, first of all, the first event that happens
09:26that we know of, and to know more deeply into this,
09:29definitely I don't wanna go too much off topic into events,
09:33because really our main focus is using events
09:36to talk about scope, is that our dog is the first thing
09:40that's stored in our scenario, even though we have a way,
09:42because the root basically goes, it goes in this direction,
09:45it starts from here, goes up here, goes back down here.
09:49Whenever we're calling the event in here,
09:52we're calling to it on its way back.
09:53So basically, when we're looking at the anims
09:55at event listener, we're putting an event right here.
09:58We're putting an event when it's on its way back.
10:00So first of all, it comes to the stage,
10:02it comes here, it comes here, then it comes right back,
10:04and it comes to us back.
10:06If we wanted to, we could have put the event up here.
10:08I'm not gonna talk about how we do it,
10:09because we have a whole video about it,
10:10a whole section on it, a very, very long section
10:13about events, which I feel you guys are ready for.
10:15So whenever you feel that you're ready for it,
10:17go and watch those two hours.
10:18They're not easy, but they're gonna make it
10:20so much more fun working with events
10:22when you get how they actually work.
10:24Now, back to our application.
10:27So first of all, that's good.
10:29We got that to work, and we know what target is.
10:32Target is the thing that triggered the event.
10:36What is the thing that triggered the event?
10:37The thing that was clicked on.
10:38So in our scenario, this is the thing
10:40that told the world I was clicked on.
10:42This is the target.
10:43Now, there's one more thing
10:45that's called a current target.
10:47So I wanna trace out our current target as well,
10:49just so we could see what it is.
10:50Current target is a little bit different than target.
10:53Current target, if I just click on this,
10:55current target is what does current target return to me?
11:00It returns to me the spray.
11:02What is the spray?
11:03The spray is this anims that we created.
11:05Now, what does, instead of me answering,
11:09let me throw this back to you guys.
11:11So we know that the target is the thing
11:12that told the world I was clicked on.
11:17I was changed, I was touched.
11:19What is the current target?
11:20What is the best way to describe what the current target is?
11:22It's returning to me the object itself,
11:24the object that we asked the vet listener to.
11:27So let's see if you guys could, you know,
11:30connect that last dot here.
11:34Oh, seems like I should answer it,
11:36but let's give it another try.
11:38But, you know, let's anyone answer.
11:39So anyone that feels like they have an answer,
11:40just throw it out in the air.
11:43And you're, well, yeah, well, exactly.
11:47So first of all, you added the animals to this
11:48and the function being called is controlling the objects.
11:52Yeah, well, both of you, Adam, Ramaz, is a great answer.
11:56So first of all, current target,
11:57what does current target do?
11:58Current target is really,
12:01is the thing that initiated,
12:02for us all we need to think about
12:03is the thing that initiated this whole thing.
12:05It's initiated the request.
12:07It's the thing that asked to listen to it.
12:11And the thing that asked to listen to it
12:12is our current target.
12:14So if I wanted to,
12:15I could have animated the current target.
12:17Well, in this scenario, it's the parent,
12:18but it didn't have to be the parent.
12:21If I wanted to, I could have said
12:22add event listener directly.
12:25And by saying add event listener,
12:26let's see what would happen.
12:27If I just click on the dog,
12:28you'll see that the objects sprite.
12:34Oh, I didn't save it.
12:36I always do that.
12:37Didn't get what I wanted.
12:38I was like, why didn't I get what I want?
12:39Run this again.
12:40I know I had to save it, click on it.
12:42And here we go.
12:43Now we're getting the main.
12:44It's always who initiated the call.
12:45It doesn't really matter if it's the parent or not.
12:47It could have even not been a parent at all.
12:49It could have been something
12:50that's not a display object at all.
12:51It doesn't have to be a display object, okay?
12:54So in our case,
12:55technically in this scenario, it's its parent.
12:57It's true that the event cycles,
13:01the order of which what gets called first
13:06in display objects is based on who's the parent.
13:09But the listener could have been anything.
13:10The listener could have been a display object.
13:12The listener could have been anything in this realm.
13:16Now, obviously the listener,
13:18the thing that's adding
13:19when you're doing the add event listener,
13:21it has to be a display object, right?
13:23So it has to be, this is the display object.
13:26So you're right.
13:27And we had to start to sending in this line of inheritance,
13:30but it could have been 10 lines up.
13:32It could have been 20, it could have been 100.
13:34We have no idea if it's a direct parent.
13:36So we can't assume that it's a parent.
13:37In this scenario, it is.
13:39All right, so that really covers the,
13:42like those that thread of scope
13:47that I wanted to connect again,
13:48now that we're really comfortable with objects,
13:50now that we're working with events.
13:52And I wanna only connect one more dot
13:54into this whole beauty.
13:56And this is just a dot to connect
13:57because our main topic isn't events,
13:59but I wanna show you how to create events, all right?
14:02So we wanna create an event.
14:05Now, until now, we've been adding event listeners,
14:07which is, it's a little bit hard sometimes,
14:09but especially getting into this whole cycle,
14:12getting into it.
14:13But I wanted to show, so how do we dispatch an event?

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!