Talking About Scope
A deeper talk about scope and seeing how it really works. In order to really master working with display objects one of the first and the most fundamental obstacle is always knowing where you are, because without that it will be very hard to tell 'things' to do 'stuff'. Let's look at how we work with the key 'this' in action and understand what it really does. We will explore the root and see how it's different than our regular operations. In this video we will call the same text filed in a few different ways practicing the concept of scope.
Video Transcript
00:00Now another just another notice now
00:02It's really important when we're starting out to work with scope and we're starting to see relationship between things
00:08You can hear the word scope quite a lot of times
00:11As you want to make sure when you're trying to figure out if something is connected to what first ask yourself
00:15What is my parent so if I'm looking at this text field, and I'm asking you? What is my parent?
00:21What is the parent of this text field? You know the parent of this text field is the root
00:25Why do I know it's the root because we are on the root?
00:29We're in the base where it has no parent above it, and we're going to see soon how to create
00:35Children's children and add children to children and so on but in this basic example
00:40we have only our first child on the screen which is
00:43Right now it's the text field because we decided to use the text field
00:47and our text field
00:50Is in the root right and because our main is the root we could access it directly by just typing text hello
00:58now
00:59Same thing would be valid and we're kind of getting a little bit more into the dot syntax as we talk about this
01:05Let's look at other ways to approach our text field
01:08One way we could approach our text field is just by saying text. Hello, which we've done that already
01:14Another option we could have done is saying this
01:17dot
01:19Text hello
01:23Why would that work and we're gonna see a lot more of this dot syntax?
01:27And we've seen it a little bit really a little bit throughout the few courses that we've done so far
01:34The dot syntax basically is relatively intuitive intuitive when you get to it
01:40basically, what I'm basically saying right here is go to this and
01:44then
01:45inside of this
01:47Take the text. Hello. So in other words, what is this? This is our main, right? So I'm asking for my main
01:55for the text. Hello property and
01:59We see that our text little property comes back
02:02Well, we'll see that as soon as we run it. Let's um, just comment out all these
02:08All these nice friends up here
02:11And let's just run it
02:12So we're gonna see the text though in the first trace and we're gonna see our text
02:15Hello on the second one which are both text fields. Let's see another way how we could approach it
02:21So if we try to trace out using our root a root again is on our stage
02:25We're at our main
02:27Root and this is exact same thing so I could do the exact same thing saying text. Hello
02:33Because our we're in the root already
02:35Thus our text. Hello is on the root if we run it one more time. We have your little error
02:41axis of a possibly undefined property text hello through a reference with
02:46static type flash display object
02:50Let's see how we could solve this issue. First of all, this is correct only
02:54for some reason well the
02:57Without getting too much in-depth into the main reason why this is not is happening because there's a type of display object
03:02Which is called display object
03:05Which is not dynamic it can have extra properties dynamically or at least it will create compiling errors to try to help us
03:13See if we have an issue now
03:15Normally, we wouldn't really want to approach our root directly and work with it
03:2099.99% we really wouldn't want to go to our root directly
03:23But if we had to go to our root, let's see a way around this issue first of all
03:28I want to I want to show one more thing before we actually solve this problem as we said before we could have approached
03:35The root directly, but the root your root
03:37Contrary to action script 2 is not a global property
03:40It doesn't you could call from anywhere you want you could only call it from visual display objects that are actually on the stage
03:47All right. So in other words our root property actually belongs
03:53to our movie clip it actually belongs to our main and
03:56The way for me to access that information I could also say this dot root
04:00It's not mandatory because it's assumed that if you don't say this that you actually mean this
04:06Or the scope that's near it and we're going to see those exceptions which are really interesting and that's where
04:11A lot of people waste a lot of hours, you know scratching their head when they can't figure something out
04:17So when we normally and you know
04:20The standard encoding is not to use this unless we really need it and we're going to see when we need it
04:24Especially in action script 3 where we assume this all the time. We assume that there's a this dot before everything
04:30It's not exactly the correct thing what I'm saying right now, but it's more or less right and I don't want to make it more
04:38Confusing than it already is right now
04:40Because these are a lot of abstract
04:43Contents. All right, so we're saying this dot root. All right, and this that root will return the same thing
04:49It should return our main which is our root
04:51Great. And now I need this type of syntax because I need to approach our route without creating a compiling error
04:58and the easiest way to do that, I'm just gonna duplicate that and
05:03The easiest way to do that is actually to after I call my root. I
05:08Could also now call if I want to call a property, but I don't want to create
05:14compiling errors
05:15The best way to create to create a call and we don't need that the best way to create a call without creating
05:21Compiling errors is actually hiding the compiler making the compiler not notice because this code is valid
05:27We know, you know, I know, you know, we've just seen that we have text. Hello on our root
05:31Now the way we could avoid our compiler and you know
05:35This is again
05:35You really shouldn't do this
05:37But I do it here and there and sometimes it's just something that is good to know and it kind of connects all the dots
05:43They explain what actually route this stage and so on are
05:48The way to access our information is going to be very similar to the way we approach objects in one of our previous lessons
05:55Or our raves
05:57We're gonna use a bracket instead of a dot and then we're gonna send a string inside of that bracket as the property name
06:03And we're gonna close the bracket what that did is basically the exact same thing as the dot notation
06:09Only it's avoiding now the compiling errors
06:12So if we run our application one more time
06:14We're gonna see that we're gonna get our text field even though we are legally not allowed to call it because it's not
06:20a property of the root
06:22by default
