1 00:00:00,510 --> 00:00:04,920 High again in this case we we're going to set a timer for our application. 2 00:00:04,920 --> 00:00:10,980 This is a way for us to visually show the time that has passed since the user started playing the game. 3 00:00:10,980 --> 00:00:16,590 So essentially if I go in here this is the timer that's going to count up the seconds as well as minutes 4 00:00:16,590 --> 00:00:17,340 for us. 5 00:00:17,340 --> 00:00:23,370 Ever since the game begins often we set the reset function or we hit the reset button. 6 00:00:23,550 --> 00:00:25,800 So let's go ahead and see how I'm going to do that. 7 00:00:25,890 --> 00:00:27,930 Feed a few defend valuables for it. 8 00:00:27,930 --> 00:00:30,920 So I go up here but my view this lot. 9 00:00:31,070 --> 00:00:33,350 And I'm going to get myself to your valuables. 10 00:00:33,360 --> 00:00:34,770 One of them is. 11 00:00:34,890 --> 00:00:37,030 I'm going to call it same time. 12 00:00:37,140 --> 00:00:40,420 And that's going to be an integer that is currently at zero. 13 00:00:40,530 --> 00:00:45,260 So that's the current game time that we have to keep adding to it. 14 00:00:45,270 --> 00:00:49,170 The other one is a timer that's going to count the time for us. 15 00:00:49,190 --> 00:00:56,010 So I'm going to call this one game time and that's going to be a timer type and I'm going to make it 16 00:00:56,010 --> 00:01:00,900 an optional value because right at the beginning it doesn't have any values essentially. 17 00:01:01,110 --> 00:01:07,810 So once I have these two value bet I have to go ahead and program my timers to actually do something. 18 00:01:07,950 --> 00:01:12,150 I'm going to do that in the view of the Lord for now but later on you're going to change it and give 19 00:01:12,150 --> 00:01:13,480 it its own function. 20 00:01:13,620 --> 00:01:19,380 So in here I'm going to go ahead and get myself a little bit more space so things are easier to view 21 00:01:19,650 --> 00:01:20,760 and in my view that lot. 22 00:01:20,800 --> 00:01:24,720 And when I go ahead let me get those as well so everything is visible perfect. 23 00:01:24,820 --> 00:01:30,340 So I'm going to go ahead and say first you have to check for something and this check is not important. 24 00:01:30,340 --> 00:01:33,180 Now it will become important later on. 25 00:01:33,240 --> 00:01:39,340 So I have to say if the game timer is currently not to. 26 00:01:39,570 --> 00:01:45,870 If you have already initialized a game timer I know that doesn't happen in the view of the Lord but 27 00:01:45,870 --> 00:01:51,780 it does happen later on and we do things such as when we reset the game because then reset the game. 28 00:01:51,780 --> 00:01:54,190 There already is a timer running. 29 00:01:54,210 --> 00:01:56,890 So we have to essentially get rid of that timer. 30 00:01:56,890 --> 00:01:58,410 If it does exist. 31 00:01:58,560 --> 00:02:05,870 So I'm going to say if it does exist then do this say game tight timer invalidate So don't run it any 32 00:02:05,880 --> 00:02:06,300 more. 33 00:02:06,330 --> 00:02:08,240 And essentially it invalidated. 34 00:02:08,340 --> 00:02:16,810 Then I'm going to go ahead and say game timeout because a timeout d'Arthur schedule fifth time intervals. 35 00:02:17,280 --> 00:02:18,950 And here's the method I want to use. 36 00:02:19,020 --> 00:02:25,500 For those of you who are taking this course as a part of the U.S. completes bootcamp then you know exactly 37 00:02:25,500 --> 00:02:26,350 how it works. 38 00:02:26,400 --> 00:02:27,860 It is totally new for you. 39 00:02:27,850 --> 00:02:30,780 This is the timer that has gone on in this site. 40 00:02:30,780 --> 00:02:37,950 In this case every second is going to go within this very file is going to look for a function that 41 00:02:37,950 --> 00:02:39,300 I haven't made yet. 42 00:02:39,300 --> 00:02:46,530 So we have to create doesn't send any use that information is essentially doesn't use any extra information 43 00:02:46,830 --> 00:02:48,820 and does repeat. 44 00:02:48,840 --> 00:02:50,140 So that's my time. 45 00:02:50,200 --> 00:02:54,830 And now I have to actually to write a function that my time is going to go to. 46 00:02:54,990 --> 00:03:01,680 So for that I'm going to get myself a little bit more space in here and say I have a function I'm going 47 00:03:01,680 --> 00:03:04,670 to call this one timer function. 48 00:03:04,670 --> 00:03:07,740 It doesn't have any problem it doesn't return anything. 49 00:03:07,830 --> 00:03:09,670 It does a few things for me. 50 00:03:09,690 --> 00:03:15,310 One of them is every time we go to a timer function it increases the time. 51 00:03:15,450 --> 00:03:17,990 So a game time increases. 52 00:03:18,060 --> 00:03:20,830 So let's say plus equal 1. 53 00:03:20,850 --> 00:03:27,330 So we get one psych and added that to game time simply because we are calculating based on every one 54 00:03:27,330 --> 00:03:27,690 site. 55 00:03:27,710 --> 00:03:33,540 And if you want to have a timer that goes that every 10 of us I can then hear you to add every tenth 56 00:03:33,600 --> 00:03:34,220 of us I can. 57 00:03:34,220 --> 00:03:39,720 Of course is going to give me an error because Game time is an integer value but you could do it if 58 00:03:39,720 --> 00:03:40,550 you want it. 59 00:03:40,650 --> 00:03:45,750 But for now I'm going to go every cycle and I'm going to add one site and every time we go there then 60 00:03:45,780 --> 00:03:49,970 I'm going to have to shoot it in a label that call the time label. 61 00:03:50,020 --> 00:03:54,040 So I'm going to go in here and say time Liebert dot text. 62 00:03:54,270 --> 00:04:00,680 And now here's something that they have to do I have to format this time to essentially look like this. 63 00:04:00,730 --> 00:04:07,130 2 spaces for the minutes a space a column and other space and two spaces for seconds. 64 00:04:07,140 --> 00:04:13,620 So I have to formatted like this and to do that I'm going to go ahead and say Give me a let let's call 65 00:04:13,620 --> 00:04:20,910 this one time a minute and the timing is going to be a string that I'm going to produce with this format 66 00:04:21,240 --> 00:04:25,350 and the format they want to use is this the string that they want to produce. 67 00:04:25,350 --> 00:04:34,460 Is from anything we tool with spaces and integer from game time. 68 00:04:34,680 --> 00:04:36,470 Divided by 60. 69 00:04:36,660 --> 00:04:39,760 So imagine if the game time is below 60. 70 00:04:39,780 --> 00:04:41,440 So that will stay at zero. 71 00:04:41,700 --> 00:04:48,760 If the game time is between 60 to 100 and 20 that would become of one that's one minute. 72 00:04:48,810 --> 00:04:53,050 For instance 90 seconds it would be 90 divided by 60. 73 00:04:53,070 --> 00:04:55,560 That is one that is integer. 74 00:04:55,560 --> 00:04:59,770 And I'm going to show one extra leading zero for it. 75 00:05:00,030 --> 00:05:08,340 Instead of saying one minute sign it would say zero and one minute sign then that's my minutes and then 76 00:05:08,340 --> 00:05:09,480 I'm going to get the sack. 77 00:05:09,480 --> 00:05:16,950 And so at say time Second is going to be the same everything except that this time you're going to get 78 00:05:16,950 --> 00:05:19,430 time modulars 60. 79 00:05:19,830 --> 00:05:23,870 And that's way I end up getting the remainder of this division. 80 00:05:23,880 --> 00:05:32,110 So real quick example if the number of seconds is 95 game time divided by 60 will produce one. 81 00:05:32,220 --> 00:05:41,160 So we get 1 minute zero or one minute and then this one is going to get 95 modulars 60 and that's going 82 00:05:41,160 --> 00:05:42,800 to be 35. 83 00:05:43,020 --> 00:05:45,710 And that is going to produce 35. 84 00:05:45,990 --> 00:05:51,880 And a second signs and that's all I have to do and I have to put them all in here boy for my formatting. 85 00:05:52,010 --> 00:05:59,490 So I'm going to say the format they want to use is this one good time minutes and then get this space. 86 00:05:59,540 --> 00:06:07,440 They call them get us space and then another format full time science and that's all we had to do to 87 00:06:07,440 --> 00:06:08,550 get this one done. 88 00:06:08,700 --> 00:06:14,190 Now I have to use my timer function in the selector and the moment they do that is going to give me 89 00:06:14,190 --> 00:06:20,360 an error saying it is not an objective c and that's a requirement of the swift version 4. 90 00:06:20,550 --> 00:06:27,750 So now it's an objective c exposed function called the timer function that happens every second ever 91 00:06:27,750 --> 00:06:31,710 since the view loads before I hit the wrong button on this. 92 00:06:31,720 --> 00:06:33,640 I'll have to fix a few things. 93 00:06:33,690 --> 00:06:41,910 I just realized one of them is that I want to shoot two leading zeros so I have to say 0 2 and 0 2. 94 00:06:42,210 --> 00:06:45,270 I forgot to show the sign for the minutes. 95 00:06:45,300 --> 00:06:50,820 So I have to save for the slash minutes and at the end of the seconds I forgot to show the sign for 96 00:06:50,820 --> 00:06:51,620 the seconds. 97 00:06:51,750 --> 00:06:52,960 So that's the seconds. 98 00:06:53,130 --> 00:07:00,840 And one last thing that they want to do is if you do this IF game time is nil is happening and we have 99 00:07:00,840 --> 00:07:09,120 to re invalidate the time it means there was some time being shown and we have to reset that simply 100 00:07:09,120 --> 00:07:13,200 because it will take one second before this thing kicks in. 101 00:07:13,200 --> 00:07:19,950 So in hearing what I say if you have to invalidate the timer then also do this say time labeled dot 102 00:07:20,110 --> 00:07:20,830 text. 103 00:07:20,880 --> 00:07:24,520 I'm here and I'm going to give it a manual a static value. 104 00:07:24,550 --> 00:07:34,380 Another static value is the zero zero for the slash minutes space column the space 0 0 and then forward 105 00:07:34,390 --> 00:07:39,410 the slash slackens and that's going to show us zero zero zero zero at the very beginning. 106 00:07:39,420 --> 00:07:46,680 Every time the game gets reset essentially it's either on the up and we should be able to see a timer 107 00:07:46,680 --> 00:07:53,210 running and showing us the right time 1 to perfect this Schwing us properly. 108 00:07:53,220 --> 00:07:57,600 Of course I don't have the reset function yet and that's something that we're going to add in the next 109 00:07:57,600 --> 00:08:02,430 lesson and on once that we should be able to see the reset actually happening. 110 00:08:02,580 --> 00:08:09,210 So this is all the code for this list and then me to reformat things real quick so we can see everything 111 00:08:09,570 --> 00:08:12,230 hopefully in just one view. 112 00:08:12,510 --> 00:08:19,440 So that is that and that is that and that's where we just get some space in here. 113 00:08:19,440 --> 00:08:26,760 So I added a timeout on all the time we reset the timer and we set a timer and we changed the value 114 00:08:26,760 --> 00:08:30,340 we see on the label every time the timer function runs. 115 00:08:30,480 --> 00:08:32,060 So let's keep it up to here. 116 00:08:32,070 --> 00:08:33,220 Let's go to our next lesson.