Cleaning up our Timer
In this video we format our text so it displays in a more standard way in our new Dynamic TextField from the last video.
Video Transcript
00:00Let's fix that.
00:00Instead of fixing it here, we want to fix it in our time
00:03now.
00:04So first of all, instead of returning an integer,
00:07I guess we're going to have to return a string.
00:11Because we're going to add 0 or trailing 0, which
00:14when integers will automatically go away.
00:16There's no such thing as 0, 1.
00:170, 1 is 1.
00:18So we have to convert it into a string.
00:22What I want to do is create another function.
00:24If we scroll down, we're going to create another private
00:28function because no one else besides us
00:29should have access to it.
00:31Let me just get this to the center of the screen.
00:36So we're going to create another private function, formats.
00:40We're going to call it format.
00:42It's going to receive an integer.
00:44So we're going to call it i because it
00:45could be the minutes, it could be the seconds.
00:47And it's going to return a string.
00:53So let's see how it works.
00:55As we format our number, when do we want to add a trailing 0?
00:59We want to add a trailing 0 only
01:01when the value is smaller than 10, right?
01:04So first of all, we're going to create a new var.
01:07We're going to call it str for string.
01:10And I'm going to, by default, first put into it something
01:13because I don't want to have there any nulls.
01:16The next step that I want to do is I want to ask if i is
01:19smaller than 10.
01:21If it is smaller than 10, then I
01:23want to add into my string 0 before I do anything.
01:28Next step, now that I've added a 0,
01:31so if our number was to be smaller than 10,
01:33then it's going to add from that value that we had there.
01:37It's going to add 0.
01:38So now it's going to have 0 inside there.
01:40And the next step is we want to add our i.
01:42So I'm going to str plus equals i.
01:46Now, the beauty in Flash is that even though i is a number
01:49and we're adding a number into our string,
01:52Flash is smart enough to realize
01:54that that number is a string, that we
01:57want to treat the number as a string,
01:58and it's going to automatically convert our number into a string.
02:01If we felt uncomfortable, we could have said,
02:03OK, this is a string.
02:06Or like most, every single thing,
02:09because we said everything is derived from an object,
02:11even primitive values, even though they're not really
02:15wrapped inside of an object, even variables
02:18that are integers have properties.
02:21And all these methods belong to the object.
02:25So if we wanted to, we could have asked to get a string value.
02:29So there's something that's called
02:30two string, which would then create our integer converted
02:35into a string.
02:36So just by calling the two string,
02:38by default, it's going to convert it
02:39into a regular 10 digit with 1 through 10 number,
02:44more likely 0 through 9, which is the 10 digits.
02:48But we don't really care about that,
02:50because we know that it's working.
02:52So now we're going to ask it to return,
02:55and we're going to return the value that's left in our string.
02:57So again, if the value was above 10,
02:59it's not going to add a trailing 0.
03:01If the value is underneath, it will.
03:04So our format should be working now.
03:06All that stuff is for us to test it out.
03:08And for us to test it out, what we want to do
03:10is instead of sending directly the value we're
03:14getting from getSeconds and getMinutes,
03:16we want to wrap it around a format.
03:19So I'm calling the format function sending that value.
03:22And I'm going to do the same thing for our getMinutes.
03:25I'm going to wrap it around that function.
03:27And our format function will be in charge
03:29of making sure that the format will be correct.
03:31Now, if we run our application just one more time,
03:33let's first correct.
03:35OK, so even though I said it would work,
03:38let's double click it and see what's not working.
03:42Oh, right.
03:42Yeah, because by mistake, I changed my total
03:45instead of changing only my seconds and minutes.
03:50My bad.
03:51That's what happens when you're in a closed booth.
03:55And here we go.
03:586, 5, 4, 3, 2, 1, and then it gets stuck, right?
04:05Because we're never finishing our complete yet.
04:09So let's complete our complete.
04:13What we want to do is if we go back to our update,
04:15we just want to add one more dispatch event.
04:17So if I just grab this dispatch event, if I wanted to,
04:22I could have called them at the same time, but I don't.
04:25I want to call only event.complete.
04:29When it's completed, we don't have to call change again,
04:31because then we could decide on our own
04:33if we want to do that change or we want to do something else.
04:37So if we're back in our application,
04:38I could just copy again also these lines.
04:41And instead of calling change, I could call now complete.
04:46And instead of on timer change,
04:47we could call it on timer complete.
04:51And on timer complete, we could do whatever we want.
04:57For example, let's just say here we're just
04:59going to put here done.
05:02We're just going to type into our text, done.
05:08And if you decided to embed the fonts,
05:11then you'd want to make sure you embed whatever copy you
05:13add into it in the last phase as well.
05:16So just so we don't have to wait so long,
05:17we're just going to comment out our 10.
05:20And we're going to test our application one more time
05:22and see if after three seconds, two, one, done.
05:28And that's exactly what we want.
05:29Now, we have only one more problem.
05:30If we run our application, we have one more problem
05:32that we're not starting from three.
05:34We're starting from zero.
05:36And the way we could fix that problem
05:37is we could call our on timer change even
05:40before it's been called.
05:42To do that, what I could do is basically, even though our,
05:45and this is one of the beauties we
05:47were talking about on null and undefined previously,
05:51null has a really cool feature that you could send null
05:56to object even though we're expecting an event.
05:59I want to call right now this on time change,
06:01because I just want to do what's inside here, right?
06:04But I just can't call it just right on like that.
06:06If I call it that way, I'm going to get an error.
06:09I can't really create a new event,
06:10because that would be crazy to just do your new event
06:14and add here some sort of something no matter what,
06:17and close that, and call it, and run it.
06:20That would work, but that's a bit too much, right?
06:23Why are we creating a new object for nothing?
06:25What we could do is we could send null.
06:27By sending null, we're kind of telling the compiler,
06:29yep, we know you're expecting that event, but we don't care.
06:35And in a way, because null is like we
06:37were saying like, what is inside of an empty object?
06:41If we create a new object and we didn't instantiate it,
06:43right, if we would just call that new object without
06:48putting anything into it, what value would come into it?
06:50Do you remember?
06:52Let's see what value would come out.
06:54Null, bingo.
06:56And the same rule applies if we would go to our new timeout
07:00and we would not start up our new timeout.
07:02If I just come into that or even call it
07:04before I instantiate it, so I'm just going to call it TO.
07:07What do you think will be in TO?
07:08TO is a type of object.
07:11So by default, its value is null.
07:13Thus, when I'm sending null to my onShymer change,
07:17when I'm basically saying it, Flash thinks that, oh, OK,
07:20it's a type of event, but it's an empty.
07:24It's not there yet.
07:25So it's acceptable.
07:27That makes sense, right?
07:29All right, so let's just.
