Don't Say It, Do it.
Misleading title i know but our point is our goal in this video is to talk about how you can avoid talking to things directly and instead talk to them indirectly such as with using the this.
Video Transcript
00:00Alright, so we've talked a lot about scope now. I want to really just wrap the scope topic because it's a very important one
00:06and we're gonna repeat it until I see you guys just are fluent with it. And the last item with scope is this.
00:11This is so important. Now,
00:14this is never something specific and this works exactly in the same way as when I was talking to you and I was asking,
00:20okay, so who does this variable belong to?
00:23So if I just go undo, undo, undo, undo, and I go back, instead of saying to who does the variable belong to,
00:28any place where I use this,
00:30you always use the same logic. You always take a step back, step back, until you find something that could own. Now, a
00:37function can own this. It's a little bit different because of this is actually, it has to be connected to an object.
00:43A function is not of this.
00:46A function is just a, it could have things in it, but it can't have a whole entity.
00:50So it's a little bit different than variables. So this has to be owned by an object.
00:54So when I go one step, when I go, whoop, whoop, whoop, when I go even one step that way, I'm going back,
01:00I'm going back, and then I'll reach it. And it really, you don't even have to do that because you know that when you're inside of
01:06your main, it's your, this is your class that you're in.
01:1199.9% of the time and we're gonna see when it's not actually today.
01:14So this, what I really encourage you to do, and I've seen your code quite a lot at this point, and
01:21one thing that I noticed you guys doing, and naturally so, is you like calling things directly, explicitly.
01:27So if I needed to call something in my, if, if for example, I'm inside of the, I'm inside of
01:34the best example, okay, this is the best example. Well, not best, but a good example.
01:39And we're not going to use here this for, in this scenario.
01:43But one thing that I also wanted to say is whenever, even before I move to the next step, whenever I say message one,
01:49I'm actually saying, Flash automatically assumes that I'm saying this dot message one.
01:55If I, I don't have to say it automatically, Flash assumes it, Flash assumes it. It searches for it there.
02:01Adobe help, oh, away help. So we know that this, this is, behind the scenes we're always there,
02:07always working with us, and we're going to try to find reasons why to use this because this
02:10becomes really useful when you actually don't know the name of your object.
02:14Or when you're, when you're sending things around, mainly in the events we're going to see this,
02:18and it's going to get really, really useful. But before we solve this issue, when we look into where
02:22could we use this, and where could it be handy, I wanted to add just like a quick event. So I'm
02:26going to add event listener. Listener. And I'm going to type the event, it's going to be just an
02:34enter frame. So it's going to be event dot enter frame. And I'm going to call it on enter. And
02:41I'm going to my function, it's going to be a private function on not function on enter.
02:51It's going to be an event type. And all I want to do is I just want to move the cat in one pixel to
02:56the right each time. So I'm going to go to the dot y plus plus equals, there we go, run my
03:01application. And so far, so good. I have no problem. Besides errors, so let's fix them.
03:09You have your access to a possibly undefined property trace. Did I not save? Yep, I did not
03:17save. So let's check that again, cannot access property method, a null object reference. Okay,
03:21so basically, it's telling me the cat doesn't exist. Let's make sure that we can actually call
03:26this variable cat. So we're gonna go properties and actually is not called cat. So we're going
03:30to make sure that it's called cat, it didn't have a name, run it one more time. And let's see
03:35what's our problem now. Let's solve it. So can I create a property on a on cat property cat on
03:41main. So let's let's solve this issue more issues. Now, the problem right now is because
03:48I created your private variable. It can't be private because it's already on the stage,
03:53anything that's on the stage is public. So because it's already defined, and the way it's
03:58defined by default, if you go into your publish settings, this is just like technicalities,
04:03that you go into your publish settings, you'll find at some point in your Swift,
04:09permit protect import. That's good. Let me just find it. Here we go. So in our advanced settings
04:15for our action script, we'll find that automatically declare stage into instances. That's a default.
04:21We want to keep the default because it's nice to have a default run my application again. But
04:25before I run it, I'm just going to change my private to be public. Because that variable is
04:29on a stage, it's public, it's public, it's available. That's how it was created, instantiated.
04:35And then I'm not having any problem now my cat is dropping from the stage. That's that's great. Now,
04:42so far, that worked for me great. Now, in my event, I have a way to talk to things without
04:48talking to them explicitly. Now, even though I'm talking here, and I'm calling cat, and I'm
04:52calling it explicitly, and there's nothing wrong with it, because it works. I have ways to
04:57approach these variables without saying when I say cat, I'm actually saying this dot cat.
05:03Now, if I wanted to create an event, and for example, in this scenario, adding the enter
05:10frame directly to my main, maybe didn't really matter if I used any other way to approach the
05:15data. But what if and let's just do this quick, I'm going to create the dog and rabbit variables
05:20as well. I want to move everybody. So I'm going to call the dog as well. And I'm going to call
05:25the rabbit as well. And now we have three variables. Now I want to move all these three
05:30variables. And I want to use three different events. So I'm going to say cat that add event
05:33listener, I'm going to call on enter, I'm going to call the same function for dog.
05:39Why not take advantage, we're going to do the exact same thing for all of them, such as a
05:42click event, such as a lot of other ways of approaching things, why create it from scratch,
05:47such as, you know, let's even let's add on top of it, let's make it even more complicated.
05:51But first, let's make it. So first, we're going to make it and then we're going to
05:53make it more complicated by it. What I want to happen is I want them to only when they're clicked,
05:58to start animating. Until they're clicked, I don't want them to animate. But now they're
06:02all going to animate. So first of all, if I just call this add event listener to all three and call
06:06the on enter, the cat is still going to move. It's just going to move three times faster.
06:10No, it's going to give me errors. Let's see why. Can I create a property rabbit on main?
06:14So let's fix that. Oh, because we have to do it all public. So we fix that as well,
06:18like we've talked about before, could delete that we're not we don't need that anymore.
06:23All right, so let's run it one more time. So here we go. Now our cat is running really
06:26quickly, because now we're moving it actually three pixels every enter frame. So that,
06:30but that's not what we want, right? That's not our solution. We want to take advantage
06:34of using events. So first of all, we want to call things without explicitly calling
06:38them anytime we can. Now, in this scenario, I could use the event and actually ask who
06:43called you? Who's the one that initiated this event? By doing that, I could just approach the
06:48target. And my event target is actually going to actually going to tell me who's the one
06:52that initiated who's the one that's who's the one that triggered that event. Now I'm not going
06:56to move the cat for a second, just so we can see it. And now if we run it, we'll see that
07:00both we'll see in our trace over and over cat dog logo, cat dog logo, cat dog logo.
07:04And this is exactly what we want. So now instead of calling cat dot y, we could now say
07:11target e dot target. And we're using the scope, we're basically passing and this really
07:17connects to scope. Because what happens here, because this private function, really its scope,
07:23it's this really belongs to the main, but we really don't want it to belong to the main,
07:27we want to work with the original scope of the original thing that we're working with.
07:32So we're asking from the event itself, what was the initial scope? What was the thing? What
07:36was the this that initiated this? Now when you're when you might, you might be thinking, well,
07:41this will when I'm here, that this is still my main. Yeah, but add event listener is a
07:46function inside of dog. So the add event listener, if I would ask you what the scope of it is, like
07:51you told me before, that is the scope of it is dog. If I was inside of the add event listener
07:56function, I would say this, I would get the dog. That's when I'm passing the this of the
08:02added event listener to the on enter, I'm getting that target. And that target is every animal
08:07separately. So now if I run the application one more time, all the animals are going to move
08:11at the same time. Now before I take in questions, and I you know, I can we get a little bit back
08:16and forth. And I want to add a little bit more level of complexity here. So we've seen how we
08:20could work with this target. And we've seen how we could work with a more dynamic way of talking to
08:24elements. And I really encourage you at this stage, revisit your older code, or newer code,
08:30and try to make sure every time you create a new event, every time you create a new function,
08:34try to think, is there a way for me not to explicitly talk to whatever I'm talking to,
08:39could I call it by using a reference that's not hard coded? Target is not hard coded,
08:45it changes depending on who's calling it. So whenever you could use something that's
08:50not hard coded, this is not hard coded either. Because this could be something else depending
08:54on where you are, right? So when you're using the not when you use things that are not
08:58hard coded, I just want to add into this also parents, when you're talking when we'll look at
09:03that into admitting parent to something that's parent, and well, parent, when you're working
09:09with visual objects, and you ask for its parent, you're not explicitly calling your parents name,
09:13you're just saying parent, instead of calling the parent directly. But before we do that,
09:17let's go and continue another next step. And the next step here, instead of adding the event
09:21listener enter frame, I'm going to change that event that I'm going to create a click event.
09:27To do that, I'm going to create another mouse event, it's going to be a click event.
09:33And I'm going to go back and down here, create another enter frame, another mouse click,
09:38another mouse click, and this time around, we're going to call it on click,
09:42going to call on click. And then we're going to create an on click function, private function,
09:50function on click, it's going to be a mouse event. And what I want to do when a mouse
09:58click happens, first of all, let's start with the cat, make it easier. When that happens,
10:03I want to start animating my cat. So as soon as that happens, I'm going to go to my cat,
10:08add event listener, and we can tell them my cat, from now on, you start enter framing. So just
10:13as we've done before, event dot enter frame, and I'm going to then call my on enter.
10:20That works so far. Now, obviously, the only problem is no matter what animal I click on
10:24my cat is going to move because they're all triggering that event. Now, obviously, well,
10:29hopefully, obviously, now that we've done this, what could I use instead of using cat
10:35to try to trigger whatever I clicked on, no matter what I clicked on, we want to move only that thing.
10:40And I'm going to throw that to you guys and ask you, what could I write here instead of cat,
10:44to make sure that no matter what I click on, the enter frame will start for that thing that
10:49I clicked on. Bingo, target. And it was target, which is actually E dot target. So thank you,
10:57Danny, E dot target. So now when I'm approaching E dot target, now I can click on the dog,
11:02and the dog is going to move and also bark bark because it's also reacting to the click that he
11:06has internally. And I could click on the cat and the cat is going to tell us that we're not,
11:10he's not happy and start animating as well. Because in the last session, we created those
11:15click events inside of those classes. And I could go to my rabbit and my rabbit is going to move.
11:21Unfortunately, not all of the rabbit is moving. All right, so because I didn't connect the whole
11:25rabbit, the background of the rabbit is there. But the point is, is clear. All right. So let
11:31me just make sure that you're all with me still. Great, Danny, how about, how about you guys,
11:37as everyone, everyone with us still, you're feeling good. Good to go. Fantastic.
