0
1
00:00:00,360 --> 00:00:05,910
So in the last lesson, we figured out how we can use a SCNAction to automate the rolling when it first
1

2
00:00:05,910 --> 00:00:07,570
gets put onto the plane.
2

3
00:00:07,570 --> 00:00:12,390
Now, I want to be able to roll all of the dice on the scene all at once.
3

4
00:00:12,450 --> 00:00:18,180
So to do that, I need to make an array that stores all of the dice that I generate.
4

5
00:00:18,180 --> 00:00:25,320
So, firstly, at the top of the file, I'm going to create a new array called diceArray.
5

6
00:00:25,320 --> 00:00:26,110
Sounds familiar
6

7
00:00:26,130 --> 00:00:29,570
if you made Dicee before. And but in this case,
7

8
00:00:29,570 --> 00:00:34,960
diceArray is going to be an array that holds SCNNode objects
8

9
00:00:35,220 --> 00:00:41,790
and I'm going to initialize it as an empty array. And then every single time that we create a diceNode,
9

10
00:00:42,150 --> 00:00:45,210
I'm going to append it to that array.
10

11
00:00:45,270 --> 00:00:53,310
So just before we put it onto the scene, I'm going to say diceArray.append, and the element I'm gonna
11

12
00:00:53,310 --> 00:00:59,820
append is diceNode, so then we're going to have an array of all of our dice nodes and we'll be able
12

13
00:00:59,820 --> 00:01:04,090
to loop through the array and spin all of them all at once.
13

14
00:01:04,290 --> 00:01:10,300
So I'm going to create a method called rollAll, and it's not going to take any parameters.
14

15
00:01:10,350 --> 00:01:19,680
Now, I'm going to say if diceArray.is Empty is false,
15

16
00:01:20,480 --> 00:01:29,600
then I'm going to say for every dice inside diceArray, let's loop through that array, and then call a
16

17
00:01:29,600 --> 00:01:35,000
function called roll that takes a single SCNNode as a parameter.
17

18
00:01:35,000 --> 00:01:39,040
So we're going to pass in that dice that we want to roll.
18

19
00:01:39,170 --> 00:01:42,860
So Xcode is gonna complain because we don't yet have this function called roll.
19

20
00:01:42,920 --> 00:01:45,530
So let's go ahead and create it.
20

21
00:01:46,190 --> 00:01:48,590
And the parameter name is going to be dice
21

22
00:01:48,590 --> 00:01:56,300
and the type of the object that it's going to accept is a SCNNode, and all we're gonna do is transplant that
22

23
00:01:56,300 --> 00:02:02,060
bit of code that we created up here. I'm going to cut it and put it inside here.
23

24
00:02:02,060 --> 00:02:06,530
So we're gonna be refactoring our code a little bit later on. But this is required so that we can actually
24

25
00:02:06,530 --> 00:02:13,070
call this functionality from multiple places, one from rollAll, but also once we've added a new
25

26
00:02:13,070 --> 00:02:14,570
diceNode to the scene.
26

27
00:02:14,720 --> 00:02:21,530
So we're gonna call roll here and the dice is going to be this newly created diceNode here.
27

28
00:02:21,530 --> 00:02:27,290
So that works, but this is giving us an error because, of course, once we put it into this method
28

29
00:02:27,290 --> 00:02:29,060
diceNode no longer exists,
29

30
00:02:29,060 --> 00:02:34,770
and instead the dice that we're going to be animating is this parameter that gets passed in.
30

31
00:02:34,790 --> 00:02:37,250
So I'll just change that to dice.
31

32
00:02:37,250 --> 00:02:43,610
So, now I'm going to go into the Main.toryboard and create a button that's going to trigger that
32

33
00:02:43,610 --> 00:02:44,970
rollAll method.
33

34
00:02:45,080 --> 00:02:51,560
So I'm going to embed our ViewController inside a Navigation Controller.
34

35
00:02:51,720 --> 00:02:58,940
And once you've done that, I'm going to add a bar button item to this nav bar.
35

36
00:02:58,970 --> 00:03:04,820
Now I'm going to link up this button to an IBAction that I'm going to call rollAgain.
36

37
00:03:04,880 --> 00:03:07,720
So control drag down here,
37

38
00:03:07,760 --> 00:03:09,910
change that outlet to an Action.
38

39
00:03:09,970 --> 00:03:12,380
I'm going to call it rollAgain.
39

40
00:03:12,380 --> 00:03:16,450
Change the type to UIBarButtonItem, hit Connect.
40

41
00:03:17,150 --> 00:03:22,260
And I also actually want to change this image because we can get a free one from Apple.
41

42
00:03:22,340 --> 00:03:26,910
I'm going to change it to the system item that represents Refresh.
42

43
00:03:27,050 --> 00:03:29,910
And this, as you can see, works as a reroll button,
43

44
00:03:29,930 --> 00:03:30,200
right?
44

45
00:03:30,530 --> 00:03:35,600
And inside this IBAction, I'm simply going to call rollAll.
45

46
00:03:39,260 --> 00:03:39,520
Now,
46

47
00:03:39,540 --> 00:03:45,540
you might be wondering why I didn't simply just put this functionality of rollAll inside the IBAction
47

48
00:03:46,880 --> 00:03:52,340
because another thing I want to do is I want to be able to shake the app and have the dice implement
48

49
00:03:52,340 --> 00:03:58,010
that rollAll functionality, and also is just better practice to be able to separate each of these discrete
49

50
00:03:58,040 --> 00:04:01,100
bits of functionality out into different methods.
50

51
00:04:01,130 --> 00:04:08,180
So let's now add our good old motionEnded, same as we did in the original Dicee. And inside here,
51

52
00:04:08,180 --> 00:04:14,460
I'm just simply going to call rollAll to roll all the dice that's currently on screen.
52

53
00:04:14,480 --> 00:04:20,810
So, now when we tap the Roll Again button, it should roll all of our dice, and also when I shake the phone,
53

54
00:04:20,840 --> 00:04:22,990
it's going to roll the dice as well.
54

55
00:04:23,420 --> 00:04:42,410
So let's give it a go.
55

56
00:04:42,650 --> 00:04:42,920
All right.
56

57
00:04:42,950 --> 00:04:44,760
So that was pretty neat, right?
57

58
00:04:44,780 --> 00:04:50,150
Well, the last bit of functionality I want to add to this app is to be able to clear the board and delete
58

59
00:04:50,330 --> 00:04:55,700
all of the dice that's already on the screen, so that we can throw down fresh dice and change the number of dice
59

60
00:04:55,700 --> 00:04:57,130
that we have on screen.
60

61
00:04:57,200 --> 00:04:59,930
So I'm going to show you how to do that in the next lesson.
61

62
00:04:59,990 --> 00:05:00,800
So see you there.
