What's a bigger one Mark Price you're at death's slopes dot com.
And let's continue building our arcade app here.
And we want to do is get these items rotating and it may not be that complex actually and then maybe
we'll do some refactoring.
So first things first is how are we going to do it.
Well we're going to need to just like you would use and spray it or seen it we're going to use an action.
And what we're going to do is create a rotary action sort of say let rotate equals LCN action.
So a scene action that repeat forever and then we'll put an action set of here seen action that rotate
by.
We want to rotate by something.
OK.
And we are going to say zero on the X but on the Y we're going to say C.G. float because we need to
cast a value here 0.01 times double dot pi.
Doubled up pi is just pi.
So it's the way you get pi you know 3.1 for blah blah blah.
And then on the z axis we're going to rotate that either one is zero and the duration we're going to
say 0.1.
So every point one of a second we want to rotate it by point zero 1 times doubled up PI along the along
the y axis and then it'll rotate it very smoothly.
So long as we run the action forever.
And so we've got the action here so what we can do now is here in our node we should just be able to
add it to that so we're going to say no dot add action or run action is what I was looking for.
Rotate and we're just going to copy this here and we're going to paste it here and we're going to paste
it here.
We're using the same action and rotating by the y axis.
And let's see if that actually works for us here.
And look at that we have rotating objects now.
We're going to see this as is.
But you could make this a little more exciting by having the rotate rotation star at a different time
period so they're all rotating differently.
They will be rotating at the same speed.
That's important and rotating at different speeds.
But you might want to do delay the rotation on these by a couple of millisecond So they they are rotating
at different angles or you could well you could also do which might even be better.
Is rotate the object after you place it just rotate it at a certain position and then have it start
from that position and it would look cool or two but we're not going to answer that you can play with
our with that you get the point.
It's not a very hard thing to do and will be a challenge for you.
So we've got two or three objects here which is really awesome.
So maybe the next thing to do is well let's think about this here we have these three objects here and
I don't like the fact that the code we're copying and pasting those three times we may not be able to
get around it but at least I think that could be a better place for that.
We might want to refactor but I think maybe instead what we can do is the tap detection whenever items
tapped we just detect it and then we grab its name and pass it on to the previous view controller.
So let's work on that feature now because we know that we're going to need it.
So here we go.
Stop it it's our computer runs nice and fast.
And here in our ramp picker VC let's just create the let's create a a gesture recognizer handler for
when the screen is actually tapped.
And we'll add a just a tap gesture to the whole scene and then we'll handle it accordingly.
So let's create a function here.
When you say at 0 BJC because we're going use a gesture recognizer which is going to use the selector
syntax we have to make this an object of C runtime function meaning it's going to use Objective-C features.
When you say func handle tap and windows can say gesture recognizer you just use recognizer and we're
just putting the underscore here.
So we don't have to give it a name and let's leave it empty for now and then what we're going to do
is actually add a gesture recognizer to the scene itself.
So we'll do it right here.
We'll say let tat equals you tap gesture recognizer and the target is going to be self and the action
is going to be selector handle tap
and then all we have to do is scene view not add gesture recognizer and let's add the taps.
Now whenever somebody taps this screen or the scene this scene gets seen.
All we have to do.
We're the scene view excuse me all we have to do is implement the code over here and handle tap.
And what do we want to do.
We want to detect which item was tapped.
Or rather we want to even see if an item was tapped and if an item was tapped.
What we want to do is get the name of that item and pass it back to the ramp.
Placers So when you go to tap that screen and knows which ramp to to to place.
OK.
So let's get started.
Let it kind of sounded like Lego Batman.
I should be like Lego Batman too.
Let's write some code.
I'm an orphan.
Let p equals an orphaned coater gesture.
Recognize in fact I should record my whole video in the voice gesture recognizer that location.
No not feeling it.
All right.
Well when you're talking to yourself for hours on end at a screen pretending that people are there he
will talk in a Batman voice to let people gesture recognize their location in eye view.
And so which view do you want the scene view.
So we want to detect the location of the tap in the scene view.
So it's going to find the location.
And it's going to store it here so we can use it and get results we're going to see if it actually hit
something.
OK that's a scene view dot hit test and be very lucky that all of this code is written for us by Apple
so we don't actually have to write great cast coding detection stuff.
This makes it really easy.
So we're going to pass and the point to see if something was hit and for the options we're going to
go ahead and just put an empty dictionary here like so.
OK.
We don't need any options hit results.
So we're going to see if anything was hit and if hit results count is greater than zero.
So if we hit an object.
OK this would have a result greater than zero.
We'd have one or one or more object in our case we're looking for one.
But what I'm saying is let's say we had five objects in the scene and we missed it.
We're even seeing if the three we missed it.
This is going to be zero and we hit nothing.
So we're going to see if anything was hit.
OK.
And if something was hit we're just going to grab the node that was hit we can grab it right here and
this is how you would do it with a game or Air care or anything when you're tapping the screen this
is how you get a 3D object.
And so you can reference it.
And what we're going to do is we're going to say let no equals hit results.
Zero note.
OK.
Where it is going to grab that node.
No big deal.
And then we're going to say switch.
We're going to check now the name of the node.
So switch no name.
And we're going to forcefully unwrap this.
And the reason why we're going to forcefully unwrap it is because we're developing we're still at compile
time and the name of the node.
OK.
In our case will always be there.
OK.
And so at compile time we have to we have to force unwrap this here.
We have to unwrap it here so we can know that it's there if it's not there or app won't even work.
Now when would you not want to force and wrap this.
OK.
Well if you were let's say had a game and let's say the game had objects that were being deleted left
and right like the lasers shooting aliens or something.
And then you went to go check the no doubt name with a note that might not even be there could have
been destroyed.
Who knows that might not had a name at that point in time.
And you could have a crash in that case you want to gracefully handle it with some some if let's regard
statements.
But in our case our app cannot function without this feature being there.
So at compile time it will crash which is what we want we want it to crash.
If it's if it's not there.
So that's why I'm Force unwrapping it.
So you'll hear me say don't ever force unwrap you know optionals or Apple crash.
Well there are cases.
Now you're becoming an advanced developer there are cases when it does not make sense to write a bunch
of code for something that has to work no matter what and you understand more as time goes by as you
program and swift So we're just checking the name of the notes so what we've done is we've tap the screen.
OK we've gotten the location story here in P.
This is the point or the location that was tapped in the screen and then what we do is we do a hit test
when you pass a point into a hit test the sync it does all the math under the hood to see if you hit
something and if it does OK the count will be greater than zero.
The results that count.
And then we grab the first element in there and we grab the note attached to it.
There's other information attached with a hit but we're grabbing the node which so if we tapped the
pyramid it would actually be grabbing the pyramid at this point in this node would be the pyramid.
And then we're just checking the name.
This is the identity that we set in the scene view when we're looking at her objects.
The node name.
And so I'm just doing a switch statement.
We're going to say case if the case is a pipe What do we want to do.
Well we want to basically tell the other view controller that we selected the pipe.
We don't have code for that yet so we're just going to break.
And if the case is quarter let's say pyramid pyramid we're going to break.
And if the case is quarter we're going to break and then the default will just we'll just leave that
empty for now.
So this is looking good.
It's yelling at us because we aren't doing anything here which is fine.
But I want to we need to do now is have some way to let the other view controller know that we we selected
something OK and that we we tap on something and then we want the other view controller to know that
that's what we're going to do.
We're going to grab that and place it on the screen.
So there's two ways of doing this.
We could do it with a protocol and become Adelia of the protocol.
We could also hold a reference to the previous view controller as well.
Either one would work just fine.
And so let's just let's just keep it simple.
First we can move to a protocol.
We need to but let's just keep it simple and hold and hold a reference to the to the controller.
So what I'm gonna do is appear at the top OK.
We go back and fix this if we want a protocol's probably the best way going forward.
But I think our app is simple enough where it doesn't need a protocol just yet.
And I'm going to keep a weak bar ramp place or VC ramp place for ABC and the reason I'm going to make
it week is because we we don't want to retain cycle is probably not going to happen but we don't want
one we don't want ramp placer VC or excuse your rent picker received a reference from place to place
reference this one with strong references because they will have a return cycle and have a memory leak.
So we're making this week here.
So rule of thumb if you don't understand this is if there are too few controllers referencing each other
or to anything too if there are two objects ever referencing each other one of them needs to be weak
or you will have a retain cycle possibly a memory leak which is which is no bueno.
And if you need more information on it just do a research do some research on iOS retain cycles and
you can learn more about it.
So ramp service.
OK what we want to do is when we're loading when we're loading the this view controller here we'll go
ahead and set it.
So what we'll do is here in our ramp place or see we will go find our pop over which is right here and
then here's our ramp picker right and so what we can do is we can say ramp picker Visi ramp placer Visi
equals self.
OK we're just sending in this view controller over here to the ramp ramp placer Durham picker and we're
setting the ramp place or other ramp picker.
If you're not confuse yet and so then what we do need to do is we need to have a function here that
we call from the ramp picker.
When an item is actually selected and I'm just going to call this function on ramp selected and let's
go ahead and just give it a rap name of type string.
So on ramp selected.
OK.
So that's good.
Let's just give it a let's give it an empty name there onramp selected.
What do you want to do.
Well that doesn't matter just yet.
We just want to implement the function here so cool again if you're going to be doing a lot of back
and forth between if you control you're going one has a protocol we're just doing one function here
so I'm not overly concerned about it.
And you can always refactor.
So wrap picker visi.
So what we're doing is in every single one of these here we're just going to pass in the name of the
item that was selected so we can work with it.
So it should be as simple as doing something like this ramp placer Visi dot on ramp selected in order
to pass in the pipe and just literally do the same thing for each one just with a different object in
there so cramped place or VCDs up on ramp selected.
And we're going to pass and the name we're going to pass in.
Pipe pyramid
and then the quarter pipe.
You know I'm wondering if we even need to do this at all on ramps like that we just pass and if we just
pass in name without even doing a switch statement that would be weightless code so we could technically
just say ramp place or AVC on ramp selected and let's just passing the node name and get rid of all
this code that we just wrote make it more better.
Yeah that'll work.
And since we don't have anything else in the scene we only have three objects.
We know it's only going be one of three names.
And so I feel good I feel okay doing this putting this in here.
And so yeah if there's is yeah we got a note here.
This is great.
I think this will work just fine.
So we'll see if something's been tapped and if it's been tapped we just pass in the name of the object
which will go over here to our ramp placer and then we can do what we want with it which is technically
grabbing building that object again and then putting on the scene and maybe you're thinking it Mark
why don't we just pass the whole note over to the ramp placer and use the same used the same object.
Well we could but you know we've got an action running on a rotate action and we also the scale and
the position are going to be off because it's different on the screen we need it full size now and I
think it's cleaner to just recreate a new one.
So I think this is great.
We've got here is we've got a function that when we tap it it works well when it works actually let's
test it here.
Let's let's go ahead and just print and we're going to say no name and see if it actually works.
How about that.
Then we'll know if it's actually working OK.
The secret button.
So they're definitely spinning.
So when I click these we should see names print out OK there does pipe pyramid quarter quarter quarter
pyramid pipe.
So it's definitely working.
Our code is working.
Look how easy it is to do hit detection with the scene.
Did all the hard work for us under the hood and we just had to implement the code here.
So really good stuff let's call this video done.
And in the next exciting video what we'll do is we'll make it so when you select this it dismisses the
view controller goes back to the other scene and then lets us place an object on the other scene.
So that's it for now.
Mark Price at death slopes Delcam moving on forward.