All right. 1
So in the last lesson, we figured out how to create 3D text to display our distance on screen, and we 2
also figured out how to position it so that it's showing just at the point where we place our endpoint. 3
So in this lesson, we're going to have a look at fixing some of the logical flaws that are in our app. 4
So currently, if you add a third dot, the distance doesn't actually change, 5
and that's because of these two lines here. 6
We created this array called dotNode that keeps track of the dots on the scene that the user has placed, 7
and every single time there's a new tab, we add another one to this array. 8
But we're only calculating the distance between the zeroth and the first element in that array. 9
So no matter how many more dots you add to the scene, you're not going to get a different distance. 10
So to fix this, we're going to add some checking methods. 11
So inside touchesBegan, what we want to do is that if there are two or more nodes inside this dotNode 12
array, then we're going to remove all the previous ones and clear the board. 13
And in this case, we're assuming that when the user touches the scene for the third time that they want 14
to clear the previous measurements and start afresh, 15
right? 16
So to do that, we're going to say if dotNodes.count >= 2, then in that case, 17
we're going to loop through the array, 18
so for dot in dotNodes, and we're going to remove each dot in the array from the parentNode, 19
so removeFromParentNode. 20
So this basically takes that 3D node off the scene and we now have no dots in the scene. 21
So if you tap once, you get one dot. If you tap twice, you get the second dot, as well as the measurement 22
of the distance. 23
But if you tap the scene a third time, then it removes the previous two dots and it shows you your brand 24
new dot. 25
And then, after all of this is done, after we've removed all the dots from the scene, then we're going 26
to clear this dotNodes array, 27
so dotNodes equals a fresh array of SCNNodes, 28
and we're going to initialize it to an empty array. 29
So let's go ahead and run and test. 30
All right. 31
So you might have noticed that our dot removing code worked as expected. 32
And whenever we tried to put on a third dot, then our code removes all the previous ones and calculates 33
a new value. 34
Now, the problem, though, is that our text is being rendered at the same position as the previous text, 35
and the previous text isn't being removed. 36
So let's go ahead and remove the textNode every single time we call updateText. 37
So to do that, we're going to have to store this textNode, not inside here, but at the top of the file. 38
So instead of saying let textNode equal this, I'm going to say textNode, and I'm going to create it 39
up here. 40
So var textNode = SCNNode. 41
And whenever we call updateText, so that's when we want to display our distance as a 3D text, the first 42
thing I'm gonna do is I'm going to say textNode.removeFromParentNode, so that clears the previous 43
text, and then we can generate a new one using our code with the latest distance. 44
So let's give it a spin. 45
And there you have it, your very own real ruler. 46
So you'll be able to measure things in the real world using your brand-new app created using ARKit. 47
I hope you enjoyed that tutorial. 48
Be sure to leave any questions in the Q & A area and also let me know if there's anything else that 49
you really want us to cover. 50
And if it's really popular, then we'll look into it. 51
So that's all from me, Angela at the London App Brewery, and I'll see you on the next lesson.