0 1 00:00:00,680 --> 00:00:00,930 All right. 1 2 00:00:00,960 --> 00:00:07,860 So in the last lesson, we figured out how to create 3D text to display our distance on screen, and we 2 3 00:00:07,860 --> 00:00:14,490 also figured out how to position it so that it's showing just at the point where we place our endpoint. 3 4 00:00:15,030 --> 00:00:20,550 So in this lesson, we're going to have a look at fixing some of the logical flaws that are in our app. 4 5 00:00:20,970 --> 00:00:26,130 So currently, if you add a third dot, the distance doesn't actually change, 5 6 00:00:26,130 --> 00:00:28,380 and that's because of these two lines here. 6 7 00:00:28,500 --> 00:00:35,490 We created this array called dotNode that keeps track of the dots on the scene that the user has placed, 7 8 00:00:36,090 --> 00:00:40,560 and every single time there's a new tab, we add another one to this array. 8 9 00:00:41,130 --> 00:00:47,770 But we're only calculating the distance between the zeroth and the first element in that array. 9 10 00:00:47,790 --> 00:00:53,150 So no matter how many more dots you add to the scene, you're not going to get a different distance. 10 11 00:00:53,460 --> 00:00:58,020 So to fix this, we're going to add some checking methods. 11 12 00:00:58,020 --> 00:01:06,870 So inside touchesBegan, what we want to do is that if there are two or more nodes inside this dotNode 12 13 00:01:06,900 --> 00:01:12,120 array, then we're going to remove all the previous ones and clear the board. 13 14 00:01:12,120 --> 00:01:18,510 And in this case, we're assuming that when the user touches the scene for the third time that they want 14 15 00:01:18,510 --> 00:01:21,710 to clear the previous measurements and start afresh, 15 16 00:01:21,750 --> 00:01:21,990 right? 16 17 00:01:22,620 --> 00:01:31,710 So to do that, we're going to say if dotNodes.count >= 2, then in that case, 17 18 00:01:31,710 --> 00:01:34,970 we're going to loop through the array, 18 19 00:01:35,040 --> 00:01:47,740 so for dot in dotNodes, and we're going to remove each dot in the array from the parentNode, 19 20 00:01:48,250 --> 00:01:50,220 so removeFromParentNode. 20 21 00:01:50,620 --> 00:01:57,790 So this basically takes that 3D node off the scene and we now have no dots in the scene. 21 22 00:01:58,180 --> 00:02:03,700 So if you tap once, you get one dot. If you tap twice, you get the second dot, as well as the measurement 22 23 00:02:03,700 --> 00:02:04,380 of the distance. 23 24 00:02:04,630 --> 00:02:10,810 But if you tap the scene a third time, then it removes the previous two dots and it shows you your brand 24 25 00:02:10,810 --> 00:02:11,260 new dot. 25 26 00:02:12,610 --> 00:02:17,920 And then, after all of this is done, after we've removed all the dots from the scene, then we're going 26 27 00:02:17,920 --> 00:02:20,470 to clear this dotNodes array, 27 28 00:02:20,500 --> 00:02:26,450 so dotNodes equals a fresh array of SCNNodes, 28 29 00:02:26,650 --> 00:02:30,120 and we're going to initialize it to an empty array. 29 30 00:02:30,400 --> 00:02:32,410 So let's go ahead and run and test. 30 31 00:03:00,940 --> 00:03:01,240 All right. 31 32 00:03:01,240 --> 00:03:06,160 So you might have noticed that our dot removing code worked as expected. 32 33 00:03:06,160 --> 00:03:12,310 And whenever we tried to put on a third dot, then our code removes all the previous ones and calculates 33 34 00:03:12,400 --> 00:03:13,600 a new value. 34 35 00:03:13,600 --> 00:03:20,560 Now, the problem, though, is that our text is being rendered at the same position as the previous text, 35 36 00:03:20,710 --> 00:03:23,160 and the previous text isn't being removed. 36 37 00:03:23,530 --> 00:03:30,400 So let's go ahead and remove the textNode every single time we call updateText. 37 38 00:03:30,400 --> 00:03:37,320 So to do that, we're going to have to store this textNode, not inside here, but at the top of the file. 38 39 00:03:37,330 --> 00:03:45,070 So instead of saying let textNode equal this, I'm going to say textNode, and I'm going to create it 39 40 00:03:45,190 --> 00:03:45,990 up here. 40 41 00:03:46,090 --> 00:03:50,440 So var textNode = SCNNode. 41 42 00:03:53,540 --> 00:04:00,110 And whenever we call updateText, so that's when we want to display our distance as a 3D text, the first 42 43 00:04:00,110 --> 00:04:07,460 thing I'm gonna do is I'm going to say textNode.removeFromParentNode, so that clears the previous 43 44 00:04:07,460 --> 00:04:12,870 text, and then we can generate a new one using our code with the latest distance. 44 45 00:04:12,950 --> 00:04:14,060 So let's give it a spin. 45 46 00:04:43,690 --> 00:04:47,320 And there you have it, your very own real ruler. 46 47 00:04:47,500 --> 00:04:53,800 So you'll be able to measure things in the real world using your brand-new app created using ARKit. 47 48 00:04:53,800 --> 00:04:55,170 I hope you enjoyed that tutorial. 48 49 00:04:55,180 --> 00:05:00,790 Be sure to leave any questions in the Q & A area and also let me know if there's anything else that 49 50 00:05:00,790 --> 00:05:02,170 you really want us to cover. 50 51 00:05:02,230 --> 00:05:04,930 And if it's really popular, then we'll look into it. 51 52 00:05:04,930 --> 00:05:09,310 So that's all from me, Angela at the London App Brewery, and I'll see you on the next lesson.