Q:But what if they don't have names?
Student question : How to add events directly to objects without giving them names, can we do it directly in there classes?
Video Transcript
00:00Okay, let's see what other questions we have here.
00:01So we have here Danny and you're asking,
00:03what I want to know is,
00:05if you are adding an event listener in a class
00:07for the main character in a game
00:09and the event is for the character movement,
00:11then would you,
00:15then you would be saying the function linked
00:18to the event in citizen name and plus or whatever,
00:20but if you are creating the class
00:22or whichever rather than movement
00:23before you actually create the instance item,
00:26then what would you refer?
00:27Beautiful, beautiful, beautiful, beautiful question.
00:29Thank you, Danny.
00:30Well, this is exactly, exactly, exactly.
00:33We're talking about scope.
00:36We said always, and this is a really perfect scenario.
00:38Now you kind of gave the answer to why would we use this?
00:42Now, when we're looking at the animal class,
00:44you said it right now, we're creating only one animal.
00:48You know, we're, and let me just,
00:50you know, I'm gonna do that just quickly.
00:51So let me just comment down here, everything,
00:53just to give a good answer to this good question.
00:55So I'm just gonna make this a guide.
01:00Let's see where else, so layer four,
01:03make that a guide as well.
01:05Still want it around.
01:07And this will be a guide as well.
01:09All right, I'm gonna create one more layer.
01:11I'm gonna call this cats.
01:14Now, I'm gonna create a lot of cats
01:17and I'm gonna get rid of the name.
01:19I'm gonna get rid of the variable
01:20and we're gonna fix this stuff in our code.
01:22Gonna hear a lot of cats.
01:24Now, I'm gonna move that movement function.
01:26I'm gonna move this.
01:28We have on that, now we have a lot of cats on the stage.
01:31I wanna move all this.
01:32I'm gonna get rid of all the events I have here.
01:34We're not gonna talk to cats directly anymore
01:36because if we do, we're gonna error.
01:38Cats don't exist anymore.
01:39There is no cat.
01:40Deleted the cat.
01:41But it is on the stage and it is an animal class.
01:45So I could go, and we already have an add event listener.
01:48Now, when I add an add event listener,
01:50and it's already there, so we don't have to recreate it,
01:53but you know what, let's recreate it anyway.
01:55So I'm gonna create another function
01:56just for the sake of this example.
01:58As we said, when I add an add event listener,
02:01how does it know?
02:02How does it know when I'm talking to this,
02:04we were saying I'm in the class,
02:05how does it know what object I'm talking to?
02:07It knows because automatically it adds this before it.
02:11What is this?
02:12This is not the class.
02:14This is the object that is going to be created
02:17from that class.
02:19That's why you can't say this
02:20outside of your constructor.
02:22So when I'm saying this add event listener,
02:24or when I'm actually saying,
02:25or when I'm saying add event listener,
02:26it's the exact same thing.
02:27When I'm saying that, I'm saying,
02:28talk to whatever object that was just created
02:32and add an event listener to it.
02:34And now I could add an on move,
02:36whatever I wanna on click, move.
02:39And then I could take that function move
02:41that we had there on entry here,
02:43and you could probably use both of these on-touching.
02:48We'll see the beauty of this.
02:49So I'm gonna take this on click,
02:51and this is gonna be really, really fun.
02:53So I'm gonna take this on click,
02:55just drop it right here.
02:57I'm gonna take it to the on enter,
02:59drop it right there.
03:00Because we didn't use names directly,
03:02look how easy it's gonna be to just move it.
03:04I didn't even have to rename anything.
03:05So I could delete all this.
03:07Don't need those traces.
03:09Don't need this trace anymore either.
03:11I don't need this trace either.
03:13All right, so as soon as my animal was clicked on,
03:17as soon as this was clicked on,
03:18I don't know what this is yet.
03:19Whatever, it's gonna be created actually
03:20a lot of times on the stage right now.
03:21It's gonna be created all these times.
03:24Whenever any of those are getting created,
03:25we're gonna take this, that object that it is right now.
03:28It's not that class anymore.
03:30We're gonna look more into that also in the future,
03:31but thank you so much for that question,
03:33because that's a really, really great topic.
03:35So with this, we're calling the on click move,
03:39on click move, or rename that on click to on click move.
03:43On click move is then called.
03:44Then we're calling the trigger, the target event,
03:47in which if we wanted to,
03:48we could have called it this as well, right?
03:50Because we're in this,
03:52there's no extra stuff inside here.
03:55We don't have to change it.
03:56It's nice using these dynamic names,
03:58because then it's easy to just copy,
03:59paste, and put it in different places
04:00and reuse the same code.
04:02So we're gonna leave it the way it is.
04:03So we're talking to this,
04:04which is whatever the name will be.
04:05We don't know what it is yet,
04:06but we have a dynamic name here.
04:09Then we're gonna call the on enter frame,
04:10and the on enter frame will do the exact same thing
04:12as it did before.
04:13So if we run the application down,
04:14hopefully we won't have errors,
04:15but of course we just are gonna fix them.
04:17So we're trying to access something that doesn't exist,
04:19that cat, so we're gonna comment the cat out.
04:22All right, so now we have a lot of stuff on the stage,
04:24and it's time for us to click around and see if they work.
04:25So if I click on cat number one,
04:27it's starting to move around.
04:28I click on two, it's moving,
04:30three, moving, four, moving.
04:31The dog is also a type of animal,
04:33so it should move as well.
04:34Our rabbit is a type of animal as well,
04:36so it should move as well,
04:37because it extends from that animal.
04:39Okay, so hopefully that answers your question,
04:42where what is that actually your question
04:44about how could you do without actually naming it
04:46is exactly the point of this.
04:48And every time that you don't say anything,
04:49you're actually saying this.
04:51So all this time when you reuse functions,
04:52this is gonna get way more clear next week,
04:54because next week we're gonna talk about
04:55static functions and static variables,
04:57and we're gonna talk more deeply about classes,
04:59which we're gonna save that for next week,
05:01for two weeks from now.
05:03Okay, so let me just go back and make sure
05:04that that was a good answer.
05:07Okay, so we have here, I see,
05:08so basically whether you're clicking on it,
05:11it has a click event applied to it, right?
05:13Well, yeah, the click event is applied to it
05:14only because we added the event listener.
05:17The add event listener, we didn't add to the class,
05:19we added it to the object that's being created.
05:21That's why we put it in the constructor, okay?
05:26How do you work with a keyboard?
05:28Let's, that's a little bit off topic.
05:30We'll go back to our game in the next half,
05:33and the next half we're reusing the keyboard,
05:35and you can see it in the next video, okay?
