So in the last lesson, we figured out how we can use a SCNAction to automate the rolling when it first 1
gets put onto the plane. 2
Now, I want to be able to roll all of the dice on the scene all at once. 3
So to do that, I need to make an array that stores all of the dice that I generate. 4
So, firstly, at the top of the file, I'm going to create a new array called diceArray. 5
Sounds familiar 6
if you made Dicee before. And but in this case, 7
diceArray is going to be an array that holds SCNNode objects 8
and I'm going to initialize it as an empty array. And then every single time that we create a diceNode, 9
I'm going to append it to that array. 10
So just before we put it onto the scene, I'm going to say diceArray.append, and the element I'm gonna 11
append is diceNode, so then we're going to have an array of all of our dice nodes and we'll be able 12
to loop through the array and spin all of them all at once. 13
So I'm going to create a method called rollAll, and it's not going to take any parameters. 14
Now, I'm going to say if diceArray.is Empty is false, 15
then I'm going to say for every dice inside diceArray, let's loop through that array, and then call a 16
function called roll that takes a single SCNNode as a parameter. 17
So we're going to pass in that dice that we want to roll. 18
So Xcode is gonna complain because we don't yet have this function called roll. 19
So let's go ahead and create it. 20
And the parameter name is going to be dice 21
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
bit of code that we created up here. I'm going to cut it and put it inside here. 23
So we're gonna be refactoring our code a little bit later on. But this is required so that we can actually 24
call this functionality from multiple places, one from rollAll, but also once we've added a new 25
diceNode to the scene. 26
So we're gonna call roll here and the dice is going to be this newly created diceNode here. 27
So that works, but this is giving us an error because, of course, once we put it into this method 28
diceNode no longer exists, 29
and instead the dice that we're going to be animating is this parameter that gets passed in. 30
So I'll just change that to dice. 31
So, now I'm going to go into the Main.toryboard and create a button that's going to trigger that 32
rollAll method. 33
So I'm going to embed our ViewController inside a Navigation Controller. 34
And once you've done that, I'm going to add a bar button item to this nav bar. 35
Now I'm going to link up this button to an IBAction that I'm going to call rollAgain. 36
So control drag down here, 37
change that outlet to an Action. 38
I'm going to call it rollAgain. 39
Change the type to UIBarButtonItem, hit Connect. 40
And I also actually want to change this image because we can get a free one from Apple. 41
I'm going to change it to the system item that represents Refresh. 42
And this, as you can see, works as a reroll button, 43
right? 44
And inside this IBAction, I'm simply going to call rollAll. 45
Now, 46
you might be wondering why I didn't simply just put this functionality of rollAll inside the IBAction 47
because another thing I want to do is I want to be able to shake the app and have the dice implement 48
that rollAll functionality, and also is just better practice to be able to separate each of these discrete 49
bits of functionality out into different methods. 50
So let's now add our good old motionEnded, same as we did in the original Dicee. And inside here, 51
I'm just simply going to call rollAll to roll all the dice that's currently on screen. 52
So, now when we tap the Roll Again button, it should roll all of our dice, and also when I shake the phone, 53
it's going to roll the dice as well. 54
So let's give it a go. 55
All right. 56
So that was pretty neat, right? 57
Well, the last bit of functionality I want to add to this app is to be able to clear the board and delete 58
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
that we have on screen. 60
So I'm going to show you how to do that in the next lesson. 61
So see you there.