02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Updating Text

Connecting the dots. In the last video we dispatched events that announced to the world that an event has just happened. In this video we are prepping a visual element on the stage to update based on the event.

Video Transcript

00:00untimer changed what we want to do is we want to get actually the amount of minutes that are left and
00:05The amount of seconds that are left and then if we had a visual display object of some sort
00:10We would want to display it on the screen
00:12All right
00:13So let's say if I wanted to do that I could just add on to my stage
00:17And I'm just gonna grab for now just a text field
00:24And I'm just gonna make sure that it's going to be one of those classic text fields one of the
00:29classic text and
00:31We want it to be dynamic. We don't want the user to select it sans sounds fine for us
00:37But if we wanted we could have picked any font we wanted in our system and embed
00:42our numbers, but for our case, we're happy with sans so
00:46all you really would have to do is just embed the numbers and
00:49embed maybe the coat the
00:52Colon and you would be good to go, but we're not gonna embed because I'm happy with the sans
00:57We're not animating it really. So we're good in that department. All I have left is just to type here. Maybe
01:030 0 0 0 and just like try to align it as much as I could code with the fonts that I have set up here
01:10And I'm gonna set it to 40 and I'm going to center it and I could say you could make it even bigger
01:15So let's make it 80
01:18And that look that works for me
01:24Weird font
01:27Because I have here an extra line for some reason so let me delete that line
01:34Fantastic and if we wanted to we could also could made our text field nice and big
01:39not that it matters, but then it would make sure that our text would be exactly in the center no matter how the rendering would be and
01:46We set by the way the format we just send center aligned it at this point now
01:50All that stuff is for me to give it a name such as
01:53t
01:54time
01:56Upper case for our second team, so we're gonna call it t time
02:00I'm gonna save it and we're gonna revisit our app and just add that variable to do that. I'm gonna create a new public
02:07Public bar which was called t time which was a type of text field and again it automatically imported it for me
02:16And all they have left right now is actually to start adding the content inside now. I'm missing two methods to do it
02:21right I'm missing one method that I'm missing is
02:24How many seconds are left on?
02:27How many minutes right now? I'm just getting it as a whole chunk of this is all the time. That's left
02:32So let me just grab this function
02:35We're gonna type it for here
02:37And I'm just gonna duplicate it twice and we're gonna call one get seconds and the next one we're gonna get minutes
02:43All right, so I did the get seconds. How are we gonna get the seconds we can do mall do
02:5160
02:52Because it's only gonna return to us the leftover from a division by 60 which that's exactly what we want
02:58And if I get minutes you want to make sure that we're gonna take our total seconds minus
03:07Total seconds mall do right we want to get rid of that and then just take this number and divide it by 60 because we
03:13know that it's gonna be a number that divides by 60 equally or
03:17We could have done something even easier. We could have just say total seconds
03:22Divide by 60 and now we just want to truncate it. We want to get rid of our
03:28Whatever is the dot we don't really care so we could use our math class with the math math dot floor
03:36Or if we wanted to we could just force it to be an integer by converting it into an integer
03:40So let's see both options and what if I just convert it into an integer
03:44I could just say take that value whatever it is turn it into an integer and by
03:49Automatically what flash will do is it's going to get rid of
03:52Anything that's trailing it's not going to round it up not going to round it down
03:55It's just gonna get rid of everything in between which is probably much quicker than actually
04:01Figuring out is the bigger as it's smaller and then truncating it, but let's run it and see what happens so
04:08That is one option, but if we wanted to we could always could have called also our math
04:12Math
04:14Floor which would basically floor the number take whatever number and just return it back as a smaller number
04:20But I'm happy with int so let's stick with int because it's a simple solution
04:25It's also what the type of value that's expected to be returned
04:28And if we go back into our app all that we really have to do left now at this point is to call our T
04:34timer
04:35Dot text or T time sorry that text and we want to set our text to be to dot
04:45Okay, so one of the one we have we're having a problem right away
04:48And you can see the problem is because when I start when I I'm not getting the hints and the reason I'm not getting
04:52The hands is because we said our variable inside of our constructor
04:57So now that we're inside of our next method our T. Oh doesn't exist anymore
05:03Or at least we don't have access to it anymore to gain access to it
05:07We have to put that we have to create that variable we have to define that variable
05:13outside of the outside of that
05:15Function basically in a class just as we were doing it when we were defining it inside of our timeout
05:21We created those two private variables, so let's do that
05:24We're going to create a new private variable because no one else besides our class will have access to it
05:30And I just copied the same values
05:33Now we don't want to define it again because if we do define it again
05:36It's going to treat this as a completely separate variable, but we don't want that to happen
05:41all we want to do is take that T. Oh and
05:44Just say okay take the T. Oh that it was defined up here and create the value and put it into it, okay?
05:51So now that we got our T. Oh we could go back down into our on timer and type our T. Oh and get that information
05:58so we want to get minutes and
06:01We want to add to it
06:03Colon and we want to add our T. Oh don't get
06:07Seconds, and we want to close that with a semicolon
06:13Now this should happen every second besides when our application is complete
06:18All right, let's run our application
06:20Just see if it at least does it for the first 10 seconds now
06:24We're going to see that we're going to have a few problems with this layout
06:27So let's see what happens first of all as soon as it starts
06:30It's getting rid of our trailing zero and our on both sides, so let's fix that

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!