MouseEvent.CLICK - A Click Event
We have a button that can roll, it changes colors, and we can even define where it will be clickable. Well, how about we check out how to actually add an action when a user clicks on our button.
Video Transcript
00:00So we created now a button from scratch, which was quite easy.
00:03Now, of course, you want to create some nicer buttons,
00:05but let's see how we could actually take that button
00:09and roll with it and actually make it do something.
00:13So let's move on to code and see how we could do that.
00:18OK, so I got my two buttons, my button move 1
00:20and my button move 2.
00:23And we're going to learn a new little concept right now
00:26enable us to actually interact with buttons.
00:30The event dispatcher enables elements to dispatch events.
00:35Now, not only are we inheriting from the event dispatcher,
00:38we're also inheriting from interactive object, which
00:41enables us to actually call interactive events, which
00:45means clicking, for example, or rolling over.
00:48Let's just do a little sample with clicking.
00:50So let's say we're going to take our button 1, move, oh,
00:55sorry, button move 1.
00:58And we want to call something that's called a function that's
01:01called add event listener.
01:04We're going to open a round bracket.
01:08And then we could scroll through the event types
01:11and find the event that we want.
01:12But we actually know we want to use something
01:14that's called a mouse event.
01:16So I'm going to type uppercase mouse.
01:18And it already selected for me, I want a mouse event.click.
01:23And you'll notice that this whole thing is an uppercase.
01:25And if you're pondering, but then you
01:27said that properties, and this is a property of the mouse event
01:30class, but then you said click, properties
01:34would be all lowercase, right, unless it's camel case.
01:38Whenever you see a property that's uppercase,
01:41it basically means that it, and that is just a standard,
01:45it basically means that it's a constant.
01:47And a constant means that it's not
01:50saying that could change.
01:51It's a constant, there's something in there,
01:54such as a string, or a number, or a Boolean,
01:57or any other type of variable that could not change anymore.
02:00It's hard coded.
02:03Typically, you would find inside of those type
02:06of static elements, you would find stuff
02:09that are primitive values, such as a string.
02:12In our scenario, adding the event
02:14is actually when we're calling mouse event.click.
02:16We're basically passing in a string that says the words
02:20click.
02:20Instead of typing it directly, it just
02:22makes it a little bit easier, so it avoids spelling errors.
02:28But what we basically did so far in this step
02:29is we approached our button.
02:32We are asking to add an event listener.
02:34We basically want to listen to the event of what?
02:37We want to listen to the event of clicking.
02:40Now what do we want to do in the event that
02:43happens when there is a click?
02:45We want to call some sort of listener, a function.
02:47We're not going to get into other details,
02:49we're going to make a nice big video about events
02:52in a separate course.
02:54But we want to call for now just one event,
02:56and we're going to call on move.
03:01That works for me.
03:02On move.
03:03Well, on click.
03:05Let's call it on click, because it's a good thing
03:07to actually, when you're working with your events,
03:09it's kind of a standard of my own,
03:11is to try to name your events kind of close to your event
03:14itself, and maybe add to what it does,
03:17because then it helps on click move.
03:20So it's easier to see what is actually the relationship.
03:25Most of the time, when you're going to create functions
03:27and you've created already functions before,
03:30you'd probably call, functions are usually actions.
03:33You'd usually call them in something that's actionable.
03:35Well, properties are stuff that are just displayable.
03:38They're stuff that are there.
03:40There isn't an action happening behind the scenes.
03:42Well, a function is basically a function.
03:45There's something going on.
03:46So that's why, usually, when we create a function,
03:48we give it a description, usually, that is functiony.
03:55It's kind of hard to describe that,
03:56but when I'm saying on click move,
03:57it kind of sounds like an action, right?
04:00On moving.
04:01On move.
04:02They are all actionable, which is a nice way
04:03of calling functions, because it kind of makes sense in the head.
04:06And when it makes sense, it makes it easier to code.
04:09All right, so let's see how we actually add that function.
04:12So far in our coding, we always added our functions
04:15inside of our constructor.
04:17But now that we're approaching our object-oriented world,
04:20and we already know that this whole baby is actually
04:23a class, and that whole baby is actually inside of our root.
04:28It's our display on the root.
04:29It's our code.
04:30There's kind of like this interconnected relationship
04:32between what's on top of the stage
04:34and what's inside of the class.
04:37Well, then we know that we want to actually add
04:39that property, that method.
04:41Let's just add it directly to our class, which basically
04:44means that that function would belong to our main class
04:48instead of belong to the constructor.
04:50Now, when we create a function that belongs to our class
04:54and not to our function, we call those functions also methods.
05:00All right, so if you see the word methods in other places,
05:02a function is not a method necessarily,
05:05but a method is a function.
05:08In other words, a function is not connected to an object,
05:11or at least we're not aware that it's connected to an object,
05:14versus a method which belongs to an object.
05:18So in our scenario, it definitely belongs,
05:20because we couldn't abstract this and take this
05:22to other places.
05:23Our onClickMove belongs to this class.
05:26Now, let's create that function.
05:27So function, and everything is basically the same thing
05:31as we've done before.
05:32We're going to give that function a name,
05:36and that name will correlate to the name
05:38that we're sending to our addEventListener.
05:41And then we're going to open a round bracket, as we did before.
05:43And we have a mandatory event.
05:46The event is coming through, so we're
05:47going to have to make sure that we leave room,
05:49that there's the first property and only property,
05:53is our mouse event.
05:54Let's find our mouse event.
05:57And it doesn't return anything, so I'm
05:58going to set it as returns void.
06:01And there we go, and we have already right now
06:03a full onClickMove.
06:05Now, if we just trace out here just
06:07to see that it's being called, I notice what I've done.
06:19I use the back, because instead of using a double quote,
06:23I use a regular quote.
06:25If I wanted to use a regular quote again,
06:27I just have to type the back slash, and that would do it.
06:33So if I just save this out and we run our application,
06:36let's see if when we click on our button one,
06:39it will generate a trace, and it does.
06:41I'm called.
06:42Definitely is being called.
06:44But if we click on our second button,
06:45it will not do anything, because we're not
06:47listening to the second button.
