1
00:00:05,860 --> 00:00:12,740
What's up everyone Mark Price here at Dev slopes dot com and let's continue building our arcade app

2
00:00:12,740 --> 00:00:13,460
here.

3
00:00:13,490 --> 00:00:18,830
And we want to do is get these items rotating and it may not be that complex actually and then maybe

4
00:00:18,830 --> 00:00:19,960
we'll do some refactoring.

5
00:00:19,960 --> 00:00:22,500
So first things first is how are we going to do it.

6
00:00:22,610 --> 00:00:27,380
Well we're going to need to just like you would use and spray it or seeing get where you need to use

7
00:00:27,380 --> 00:00:28,400
an action.

8
00:00:28,490 --> 00:00:37,350
And what we're going to do is create a rotate action sorgen say let rotate equals LCN action so a scene

9
00:00:37,350 --> 00:00:46,240
could action that repeat for ever and then we'll put an action set of here CN action that rotate by.

10
00:00:46,250 --> 00:00:54,010
We want to rotate by something OK and we are going to say 0 on the X but on the y.

11
00:00:54,170 --> 00:01:00,770
We're going to say C.G. flow because we're going to need to cast a value here 0.01 times double dot

12
00:01:00,890 --> 00:01:05,410
PI doubled up high as just pi.

13
00:01:05,500 --> 00:01:08,780
So it's the way you get pi you know 3.1 for blah blah.

14
00:01:09,150 --> 00:01:14,360
And then on the z axis we're going to rotate that either one is zero and the duration we're going to

15
00:01:14,370 --> 00:01:15,510
say 0.1.

16
00:01:15,510 --> 00:01:24,030
So every point of a second we want to rotate it by point 0 1 times double that PI along the along the

17
00:01:24,030 --> 00:01:28,440
y axis and then it will rotate it very smoothly.

18
00:01:28,440 --> 00:01:30,200
So long as we run the action forever.

19
00:01:30,330 --> 00:01:39,150
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

20
00:01:39,150 --> 00:01:48,580
add it to that so we're going to say no dot add action or run action is what I was looking for.

21
00:01:48,650 --> 00:01:59,270
Rotate and we're just going to copy this here and we're going to paste it here and we're going to paste

22
00:01:59,270 --> 00:02:02,130
it here.

23
00:02:02,400 --> 00:02:05,790
We're using the same action and rotating by the y axis.

24
00:02:05,800 --> 00:02:08,640
And let's see if that actually works for us here.

25
00:02:08,770 --> 00:02:13,200
And look at that we have rotating objects.

26
00:02:13,200 --> 00:02:16,020
Now we're going to leave this as is.

27
00:02:16,150 --> 00:02:22,330
But you could make this a little more exciting by having the rotating rotation star at a different time

28
00:02:22,330 --> 00:02:24,860
period so they're all rotating differently.

29
00:02:24,880 --> 00:02:26,490
They will be rotating at the same speed.

30
00:02:26,500 --> 00:02:28,320
That's important want to rotating at different speeds.

31
00:02:28,330 --> 00:02:37,060
But you might want to do delay the rotation on these by a couple of millisecond So they they are rotating

32
00:02:37,150 --> 00:02:41,250
at different angles or you could well you could also do which might even be better.

33
00:02:41,330 --> 00:02:46,510
Is rotate the object after you place it just rotate it at a certain position and then have it start

34
00:02:46,510 --> 00:02:49,780
there from that position and it would look cool or two but we're not going to answer that you can play

35
00:02:49,780 --> 00:02:51,130
with or with that you get the point.

36
00:02:51,130 --> 00:02:53,410
It's not a very hard thing to do and will be a challenge for you.

37
00:02:53,410 --> 00:02:57,280
So we've got two three objects here which is really awesome.

38
00:02:57,280 --> 00:03:06,670
So maybe the next thing to do is well think about this here we have these three objects here and I don't

39
00:03:06,670 --> 00:03:10,480
like the fact that the code we're copying and pasting the three times we may not be able to get around

40
00:03:10,480 --> 00:03:13,650
it but at least I think there could be a better place for that so we might want to refactor.

41
00:03:13,780 --> 00:03:18,760
But I think maybe instead what we can do is the tap detection whenever items tapped we just detect it

42
00:03:19,030 --> 00:03:22,600
and then we grab its name and pass it on to the previous view controller.

43
00:03:22,600 --> 00:03:25,410
So let's work on that feature now because we know that we're going to need it.

44
00:03:25,420 --> 00:03:28,480
So here we go.

45
00:03:28,510 --> 00:03:29,140
Stop it.

46
00:03:29,140 --> 00:03:31,040
Our computer runs nice and fast.

47
00:03:31,480 --> 00:03:45,010
And here in our ramp picker Visi let's just create the let's create a a gesture recognizer handler for

48
00:03:45,010 --> 00:03:52,450
when the screen is actually tapped and we'll add a just a tap gesture to the whole scene and then we'll

49
00:03:52,450 --> 00:03:53,910
handle it accordingly.

50
00:03:53,920 --> 00:03:55,840
So let's create a function here.

51
00:03:55,840 --> 00:04:01,480
When you say at 0 BJC because we're gonna use a gesture recognizer which is going to use the selector

52
00:04:01,480 --> 00:04:06,820
syntax we have to make this an object of C runtime function meaning it's going to use Objective-C features.

53
00:04:06,820 --> 00:04:15,640
When you say func handled tap and windows can say gesture recognizer you gesture recognizer and we're

54
00:04:15,640 --> 00:04:17,310
just putting the underscore here.

55
00:04:17,530 --> 00:04:22,540
So we don't have to give it a name and let's leave it empty for now.

56
00:04:22,600 --> 00:04:26,550
And then what we're going to do is actually add a gesture recognizer to the scene itself.

57
00:04:26,560 --> 00:04:29,610
So we'll do it right here.

58
00:04:29,830 --> 00:04:39,010
We'll say let tap equals you tap gesture recognizer and the target is going to be self and the action

59
00:04:39,520 --> 00:04:42,630
is going to be selector handle tap

60
00:04:46,320 --> 00:04:52,970
and then all we have to do is scene view not add gesture recognizer and let's add the taps.

61
00:04:52,980 --> 00:04:56,620
Now whenever somebody taps this screen or the scene.

62
00:04:56,780 --> 00:04:59,110
OK this scene kit scene.

63
00:04:59,340 --> 00:05:00,160
All we have to do.

64
00:05:00,180 --> 00:05:04,600
We're the scene view excuse me all we have to do is implement the code over here and handle tap.

65
00:05:04,680 --> 00:05:05,530
And what do we want to do.

66
00:05:05,550 --> 00:05:07,980
We want to detect which item was tapped.

67
00:05:08,220 --> 00:05:11,970
Or rather we want to even see if an item was tapped and if an item was tapped.

68
00:05:12,060 --> 00:05:17,330
What we want to do is get the name of that item and pass it back to the ramp.

69
00:05:17,340 --> 00:05:21,840
Placers So when you go to tap that screen and knows which ramp to to to place.

70
00:05:21,840 --> 00:05:22,460
OK.

71
00:05:23,040 --> 00:05:25,470
So let's get started.

72
00:05:25,800 --> 00:05:28,400
Let it kind of sound like Lego Batman.

73
00:05:28,440 --> 00:05:30,740
I should be in like Lego Batman 2.

74
00:05:31,240 --> 00:05:33,270
Let's write some code.

75
00:05:33,300 --> 00:05:34,770
I'm an orphan.

76
00:05:34,770 --> 00:05:38,230
Let p equals an orphaned coater gesture.

77
00:05:38,340 --> 00:05:44,730
Recognize in fact I should record my whole videos in a voice gesture recognizer that location.

78
00:05:45,220 --> 00:05:46,750
Now I'm feeling it.

79
00:05:47,110 --> 00:05:47,530
All right.

80
00:05:47,650 --> 00:05:51,610
Well when you're talking to yourself for hours on end at a screen pretending that people are there he

81
00:05:51,620 --> 00:05:58,840
will talk in a Batman voice to let the gesture recognize their location in view.

82
00:05:59,140 --> 00:06:01,080
And so which view do you want the scene view.

83
00:06:01,210 --> 00:06:05,130
So we want to detect the location of the tap in the scene view.

84
00:06:05,140 --> 00:06:06,740
So it's going to find the location.

85
00:06:06,970 --> 00:06:12,190
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

86
00:06:12,190 --> 00:06:12,810
something.

87
00:06:12,950 --> 00:06:13,600
OK.

88
00:06:13,660 --> 00:06:21,730
When I say scene view dot hit test and be very lucky that all of this code is written for us by Apple

89
00:06:21,730 --> 00:06:23,150
so we don't actually have to write.

90
00:06:23,340 --> 00:06:25,300
Great cast coded detection stuff.

91
00:06:25,300 --> 00:06:26,510
This makes it really easy.

92
00:06:26,590 --> 00:06:30,970
So we're going to pass and the point to see if something was hit and for the options we're going to

93
00:06:30,970 --> 00:06:38,460
go ahead and just put an empty dictionary here like so.

94
00:06:39,190 --> 00:06:39,680
OK.

95
00:06:39,850 --> 00:06:42,250
We don't need any options hit results.

96
00:06:42,250 --> 00:06:48,510
So we're going to see if anything was hit and if hit results count is greater than zero.

97
00:06:48,520 --> 00:06:53,480
So if we hit an object OK this would have a result greater than zero.

98
00:06:53,500 --> 00:06:58,260
We'd have one one or more object in our case we're looking for one.

99
00:06:58,330 --> 00:07:02,180
But what I'm saying is let's say we had five objects in the scene and we missed it.

100
00:07:02,230 --> 00:07:04,280
Where even are seem the three we missed it.

101
00:07:04,300 --> 00:07:06,580
This is going to be zero and we hit nothing.

102
00:07:06,580 --> 00:07:09,600
So we're going to see if anything was hit.

103
00:07:09,840 --> 00:07:10,400
OK.

104
00:07:10,510 --> 00:07:14,800
And if something was hit we're just going to grab the node that was hit we can grab it right here and

105
00:07:14,800 --> 00:07:18,460
this is how you would do with a game or air or anything when you're tapping the screen this is how you

106
00:07:18,460 --> 00:07:19,650
get a 3D object.

107
00:07:19,660 --> 00:07:21,180
And so you can reference it.

108
00:07:21,550 --> 00:07:30,880
And what we're going to do is we're going to say let Noad equals hit results zero note care where it

109
00:07:30,880 --> 00:07:32,770
is going to grab that node.

110
00:07:32,860 --> 00:07:34,140
No big deal.

111
00:07:34,780 --> 00:07:36,650
And then we're going to say switch.

112
00:07:37,300 --> 00:07:40,000
We're going to check now the name of the node.

113
00:07:40,230 --> 00:07:44,760
So switch node name and we're going to forcefully unwrap this.

114
00:07:44,950 --> 00:07:49,570
And the reason why we're going to forcefully unwrap it is because we're developing we're Sylhet compile

115
00:07:49,570 --> 00:07:52,350
time and the name of the node.

116
00:07:52,510 --> 00:07:53,350
OK.

117
00:07:53,590 --> 00:07:55,480
In our case we'll always be there.

118
00:07:55,720 --> 00:07:56,370
OK.

119
00:07:56,650 --> 00:08:02,380
And so at compile time we have to we have to force on wrap this here or we have to unwrap it here so

120
00:08:02,380 --> 00:08:05,680
we can know that it's there if it's not there or app won't even work.

121
00:08:05,690 --> 00:08:08,870
Now when would you not want to force and raptus OK.

122
00:08:09,040 --> 00:08:14,830
Well if you were let's say had a game and let's say the game had objects that were being deleted left

123
00:08:14,830 --> 00:08:17,620
and right like the lasers shooting aliens or something.

124
00:08:17,710 --> 00:08:18,080
OK.

125
00:08:18,250 --> 00:08:22,180
And then you went to go check the no name with a note and might not even be there could have been destroyed.

126
00:08:22,180 --> 00:08:24,980
Who knows that might not had a name at that point in time.

127
00:08:25,240 --> 00:08:30,420
And you could have a crash in that case you want to gracefully handle it with some some if let's regard

128
00:08:30,430 --> 00:08:31,210
statements.

129
00:08:31,210 --> 00:08:35,370
But in our case our app cannot function without this feature being there.

130
00:08:35,400 --> 00:08:39,760
So at compile time it will crash which is what we want we want it to crash.

131
00:08:39,760 --> 00:08:41,500
If it's if it's not there.

132
00:08:41,620 --> 00:08:43,170
So that's why I'm Force unwrapping it.

133
00:08:43,270 --> 00:08:48,150
So you because you'll hear me say don't ever force unwrap you know optionals because or Apple crash

134
00:08:48,160 --> 00:08:52,000
Well there are cases now you're becoming an advanced developer there are cases when it does not make

135
00:08:52,000 --> 00:08:57,640
sense to write a bunch of code for something that has to work no matter what and you understand more

136
00:08:57,640 --> 00:09:00,620
as time goes by as you program and switch.

137
00:09:00,620 --> 00:09:05,250
So we're just checking the name of the notes what we've done is we've tapped the screen.

138
00:09:05,250 --> 00:09:08,140
OK we've gotten the location story here in ppi.

139
00:09:08,170 --> 00:09:10,850
This is the point or the location that was tapped in the screen.

140
00:09:11,020 --> 00:09:15,030
And then what we do is we do a hit test when you pass a point into a hit test.

141
00:09:15,140 --> 00:09:20,020
It does all the math under the hood to see if you hit something and if it does OK the cap will be greater

142
00:09:20,020 --> 00:09:20,660
than zero.

143
00:09:20,680 --> 00:09:25,930
The hit results that count and then we grab the first element in there and we grab the note attached

144
00:09:25,930 --> 00:09:26,080
to it.

145
00:09:26,080 --> 00:09:30,760
There's other information attached with a hit but we're grabbing the node which so if we tapped the

146
00:09:30,760 --> 00:09:34,700
pyramid it would actually be grabbing the pyramid at this point in this node would be the pyramid.

147
00:09:34,930 --> 00:09:36,160
And then we're just checking the name.

148
00:09:36,160 --> 00:09:40,150
This is the identity that we set in the scene view when we looking at her objects.

149
00:09:40,150 --> 00:09:41,870
The node name.

150
00:09:42,040 --> 00:09:43,770
So I'm just doing a switch statement.

151
00:09:43,940 --> 00:09:49,490
Organise a case if the case is a pipe What do we want to do.

152
00:09:49,740 --> 00:09:55,900
Well we want to basically tell the other view controller that we selected the pipe.

153
00:09:55,930 --> 00:09:56,910
We don't have code for that yet.

154
00:09:56,920 --> 00:10:06,160
So we're just going to break and if the case is quarter let's say pyramid pyramid we're going to break

155
00:10:07,170 --> 00:10:17,280
in if the case is quarter we're going to break and then the default will just we'll just leave that

156
00:10:17,280 --> 00:10:18,390
empty for now.

157
00:10:23,870 --> 00:10:25,310
So this is looking good.

158
00:10:25,690 --> 00:10:28,700
It's yelling at us because we aren't doing anything here which is fine.

159
00:10:28,880 --> 00:10:39,680
But I want to we need to do now is have some way to let the other view controller know that we we selected

160
00:10:39,680 --> 00:10:46,820
something OK and that we we tap on something and then we want the other view controller to know that

161
00:10:47,390 --> 00:10:48,400
that's what we're going to do.

162
00:10:48,560 --> 00:10:50,840
We're going to grab that item and place it on the screen.

163
00:10:51,010 --> 00:10:53,330
And so there's two ways of doing this.

164
00:10:53,600 --> 00:10:58,280
We could do it with a protocol and become Adelia of the protocol.

165
00:10:58,280 --> 00:11:02,930
We could also hold a reference to the previous view controller as well.

166
00:11:02,930 --> 00:11:05,860
Either one would work just fine.

167
00:11:05,900 --> 00:11:08,410
And so let's just let's just keep it simple.

168
00:11:08,420 --> 00:11:09,530
First we can move to a protocol.

169
00:11:09,530 --> 00:11:14,780
We need to but let's just keep it simple and hold and hold a reference to the to the controller.

170
00:11:14,780 --> 00:11:17,650
So what I'm gonna do is appear at the top OK.

171
00:11:17,930 --> 00:11:21,790
We go we go back and fix this if we want a protocol's probably the best way going forward.

172
00:11:21,800 --> 00:11:25,830
But I think our app is simple enough where it doesn't need a protocol just yet.

173
00:11:26,160 --> 00:11:35,980
And I'm going to keep a weak more ramp place or VC ramp place or ABC and the reason I'm going to make

174
00:11:35,980 --> 00:11:42,100
it week is because we we don't want to retain cycle is probably not going to happen but we don't want

175
00:11:42,100 --> 00:11:48,010
one we don't want ramp or VC or executing ramp picker received a reference ramp place from Placer to

176
00:11:48,010 --> 00:11:52,990
reference this one with strong references because then we'll have retain cycle and have a memory leak.

177
00:11:53,110 --> 00:11:54,620
So we're making this week here.

178
00:11:54,700 --> 00:12:00,040
So rule of thumb if you don't understand this is if there are to view controllers referencing each other

179
00:12:00,100 --> 00:12:05,850
or to anything to if there are two objects ever referencing each other one of them needs to be weak

180
00:12:06,340 --> 00:12:11,670
or you will have a retain cycle possibly a memory leak which is which is no bueno.

181
00:12:11,680 --> 00:12:17,170
And if you need more information on it just do a research and research on iOS retain cycles and you

182
00:12:17,170 --> 00:12:18,770
can learn more about it.

183
00:12:18,970 --> 00:12:28,180
So ramp placer VC okay what we want to do is when we're loading when we're loading the this controller

184
00:12:28,180 --> 00:12:29,440
here we'll go ahead and set it.

185
00:12:29,570 --> 00:12:41,490
So what we'll do is here in our ramp place or TVC we will go find our pop over which is right here and

186
00:12:41,490 --> 00:12:49,430
then here's our ramp picker right and so what we can do is we can say ramp picker Visi ramp placer Visi

187
00:12:49,450 --> 00:12:50,470
equals self.

188
00:12:50,640 --> 00:12:59,040
Okay we're just sending in this view controller over here to the ramp ramp played for Durham picker

189
00:12:59,790 --> 00:13:01,820
and we're setting the ramp her or other ramp picker.

190
00:13:01,830 --> 00:13:07,920
If you're not confuse yet and so then what we do need to do is we need to have a function here that

191
00:13:07,920 --> 00:13:10,350
we call from the ramp picker.

192
00:13:10,350 --> 00:13:21,310
When an item is actually selected and I'm just going to call this function onramp selected and let's

193
00:13:21,310 --> 00:13:27,140
go ahead and just give it a rap name of type string.

194
00:13:27,670 --> 00:13:30,300
So on ramp selected.

195
00:13:30,460 --> 00:13:31,220
OK.

196
00:13:31,460 --> 00:13:33,130
So that's good.

197
00:13:33,140 --> 00:13:37,710
Let's just give it a let's give it an empty name there onramp selected.

198
00:13:37,720 --> 00:13:38,610
What do you want to do.

199
00:13:38,650 --> 00:13:40,320
Well that doesn't matter just yet.

200
00:13:40,330 --> 00:13:46,690
We just want to implement the function here so cool again if you're going to be doing a lot of back

201
00:13:46,690 --> 00:13:50,230
and forth between if you control you're going to want as a protocol we're just doing one function here

202
00:13:50,230 --> 00:13:52,450
so I'm not overly concerned about it.

203
00:13:52,690 --> 00:13:54,290
And you can always refactor.

204
00:13:54,400 --> 00:13:56,260
So rap picker visi.

205
00:13:56,320 --> 00:14:02,500
So what we're doing is in every single one of these here we're just going to pass in the name of the

206
00:14:02,500 --> 00:14:05,770
item that was selected so we can work with it.

207
00:14:05,800 --> 00:14:15,850
So it should be as simple as doing something like this ramp placer Visi dot on ramp selected or passing

208
00:14:15,880 --> 00:14:21,280
the pipe and just literally do the same thing for each one just with a different object in there so

209
00:14:21,690 --> 00:14:25,550
cramped place or VCDs up on ramp selected.

210
00:14:26,290 --> 00:14:32,730
And we're going to pass in the name.

211
00:14:32,870 --> 00:14:34,780
We're going to pass and pipe pyramid

212
00:14:37,540 --> 00:14:39,190
and then the quarter pipe.

213
00:14:39,820 --> 00:14:45,940
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

214
00:14:45,940 --> 00:14:51,490
pass in the name without even doing a switch statement that would be weightless code so we could technically

215
00:14:51,490 --> 00:15:00,430
just say ramp place or AVC on ramp selected and let's just passing the node name and get rid of all

216
00:15:00,430 --> 00:15:04,210
this code that we just wrote make it mo better.

217
00:15:05,840 --> 00:15:06,690
Yeah that'll work.

218
00:15:06,700 --> 00:15:10,390
And since we don't have anything else in the scene we only have three objects.

219
00:15:10,390 --> 00:15:12,220
We know it's only going be one of three names.

220
00:15:12,220 --> 00:15:15,230
And so I feel good I feel okay doing this putting this in here.

221
00:15:15,490 --> 00:15:20,490
And so yeah if there's is yeah we got a note here this is great.

222
00:15:20,530 --> 00:15:21,910
I think this will work just fine.

223
00:15:21,910 --> 00:15:25,690
So we'll see if something's been tapped and if it's been tapped we just pass in the name of the object

224
00:15:26,410 --> 00:15:30,280
which will go over here to our ramp placer and then we can do what we want with it which is technically

225
00:15:30,280 --> 00:15:35,500
grabbing building that object again and then putting on the scene and maybe you think it will mark why

226
00:15:35,500 --> 00:15:43,130
don't we just pass the whole note over to the ramp placer and use the same used the same object.

227
00:15:43,130 --> 00:15:49,040
Well we could but you know we've got an action running on a rotate action and we also the scale and

228
00:15:49,040 --> 00:15:53,090
the position are going to be off because it's different on the screen we need it full size now and I

229
00:15:53,090 --> 00:15:56,700
think it's cleaner to just recreate a new one.

230
00:15:56,900 --> 00:15:58,730
So I think this is great.

231
00:15:58,760 --> 00:16:05,440
We've got here is we've got a function that when we tap it it works well does when it works actually

232
00:16:05,450 --> 00:16:06,650
let's test it here.

233
00:16:06,650 --> 00:16:16,280
Let's let's go ahead and just print and we're going to say no name and see if it actually works.

234
00:16:16,280 --> 00:16:17,010
How about that.

235
00:16:17,060 --> 00:16:18,880
Then we'll know if it's actually working.

236
00:16:19,400 --> 00:16:20,130
OK.

237
00:16:20,520 --> 00:16:22,460
The secret button.

238
00:16:22,530 --> 00:16:23,640
So they're definitely spinning.

239
00:16:23,640 --> 00:16:26,080
So when I click these we should see names print out OK.

240
00:16:27,920 --> 00:16:28,350
There it is.

241
00:16:28,350 --> 00:16:35,470
Pipe pyramid quarter quarter quarter pyramid pipe.

242
00:16:35,520 --> 00:16:36,450
So it's definitely working.

243
00:16:36,450 --> 00:16:37,590
Our code is working.

244
00:16:37,590 --> 00:16:41,120
Look how easy it is to do hit detection with the scene.

245
00:16:41,140 --> 00:16:44,240
Apple did all the hard work for us under the hood and we just had to implement the code here.

246
00:16:44,240 --> 00:16:47,440
So really good stuff let's call this video done.

247
00:16:47,460 --> 00:16:52,420
And in the next exciting video what we'll do is we'll make it so when you select this it dismisses the

248
00:16:52,420 --> 00:16:58,230
view controller goes back to the other scene and then lets us place an object on the other scene.

249
00:16:58,230 --> 00:16:59,880
So that's it for now.

250
00:16:59,880 --> 00:17:02,370
Mark Price at death slopes Delcam moving on forward.
