Variable Scope
To get our timer really ready to work we need to enable it to update externally. So lets see how we get the first steps in this ready.
Video Transcript
00:01So we got our text tool set up and everything is good on this department.
00:04Now, all that's really left for us to make our application ready for production is two
00:10things.
00:11One, I want to make sure that first of all, I don't want my main application to worry
00:15at all about anything.
00:16I'm not doing any animation or anything.
00:19All I really want to care about is that, one, is I could give it the time externally.
00:24And the second thing is that I could externally tell it what will be the ending message.
00:27And the ending message I want it to be is going to be basically just the name of
00:30the course.
00:31So in this scenario, I'm just going to give it just a random name.
00:34So I'm going to get course name.
00:39And that course name will change based on the section that we were in.
00:42And we're going to watch in a future video about how do we pass that information through
00:45Flash with something which is called FlashVars, which you can always look it up, but we will
00:52get into that in a future release.
00:53All right, but for now, we're going to assume that everything is baked in our application.
00:59Except now that we've added a new attribute, a new property, a new second parameter for
01:04our function, for our init function, we have to go to our MC timeout or our class and make
01:10sure that our init function has that value as well.
01:14So our second value here will be a message.
01:19Let's just call it what you could call a message, which will be a type of string.
01:23Now I'm going to set it a default value and that default value will be done just in case
01:28someone does not send a message or I do not send a message.
01:32Now what I want to do next is I actually want to save that message value.
01:36To do that, I could do two things.
01:37One is I really like the name of the variable message and I want to keep that variable
01:40name message.
01:42Only problem is that in this scope, if I create now another variable and I call it
01:47private var message, which will be a string, if I just create another variable, how could
01:53I say this message go inside of this message, right, because they're completely the same
01:59name.
02:00How do I reference them?
02:01The way you would reference them is not that hard.
02:03What you would do, you remember that word this that we worked with before, a word this
02:07basically lets us know who are we trying to talk to.
02:11I'm trying to talk to the display object itself.
02:15What am I trying to talk to?
02:16To the message property.
02:19Now that I know I want to talk to the message property, what do I want to tell
02:21it?
02:22I want to take this message and put it into it.
02:25Now this might be a little bit confusing, but let's try to break this down a little
02:28bit.
02:29If I'm saying that message, if I just call out message directly and I trace out message,
02:33what value would come back?
02:35First of all, it would look inside of the scope of the function that you're in.
02:38If there is any definition of message, and there is, so it would return done.
02:43If it couldn't find any definition, it would go a step up and ask basically the class
02:47itself or the object itself, do you have a variable, do you have a property that's
02:51called message?
02:53As soon as it found it, it will use it.
02:55In this case, in this scenario, because we both have an instance name, we both have
03:01a variable in our timeout display, and we have a variable inside of our function
03:06itself, we're going to tell our function variable to equal to this, our class itself,
03:14our object itself, messages variable.
03:18And that's all we have to do to pass over that information and use the exact
03:21same name.
03:22Now, if you still don't feel very comfortable with that, you don't have to call
03:25your message the same name.
03:27You could give your variable on top a different name.
03:29But it's a good practice, and don't worry about it, because this is, we said
03:32we're not learning anything new in this round.
03:35We'll definitely revisit this concept and go a little bit deeper into it in
03:38future videos as we explore deeper the workings of object-oriented programming.
03:44So the next step is I want to grab my message variable, which now will have
03:47scope in this function, and it'll be the scope of our external message
03:51variable, right?
03:52So the next thing that I want to do, I just want to scroll down to our
03:55on time or complete, and just change the copy to be our message, which by
03:59default would equal to done, and if not, if we send something in, it will
04:03change that message and output that message into our ttime when the timeout
04:08is completed.
04:10One other thing that we want to do is we want to make sure that our layout,
04:14even though it will be in our scenario, just to be safe, if in the future we
04:17change the layout of our copy, we want to make sure that our ttimeout
04:22layout will be correct as well.
04:23So I'm just going to copy that line that we've created earlier to make
04:26sure that it'll be correct also when we update our copy, such as we
04:30might have a copy that breaks down and goes into two lines.
04:34Maybe our copy is going to be longer than one line, all right?
04:37So if in this scenario, if I just take this little element and just
04:42change it to three again, so we're going to have the three, two, one, and
04:47hopefully we're going to get our course name.
04:49Now for example, what if we wanted to break that down into two lines by
04:52adding that value into that, now we're going to tell Flash to break a line
04:56by adding it inside of a string, the backslash n, and if I run my
05:01application one more time, now my application is still going to stay
05:06consistent and it's still going to keep my layout because I added that
05:09extra line here that made sure that even when our time out is complete,
05:13make sure after you render the copy that your layout is accurate.
