Created
August 6, 2020 20:47
-
-
Save analogpotato/96348e13dfe54e0d858270cb896a3868 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The issue I'm running into is that I'm adding D6DiceViews to an array and then | |
| // attempting to randomly change the number on each die | |
| //Here is the struct for the D6DiceView, it contains the function for the random roll (this is the full current view) | |
| struct D6DiceView: View { | |
| @Binding var rolledValue: String | |
| var body: some View { | |
| VStack { | |
| ZStack { | |
| RoundedRectangle (cornerRadius: 10) | |
| .frame(width: 100, height: 100) | |
| .foregroundColor(.red) | |
| Text(rolledValue) | |
| .font(.title) | |
| .foregroundColor(.white) | |
| } | |
| } | |
| } | |
| func d6rollButtonPressed() { | |
| let randomInt = Int.random(in: 1..<7) | |
| rolledValue = "\(randomInt)" | |
| print("Random number is \(randomInt)") | |
| } | |
| } | |
| //This is the "main" view where all the dice would be held/ | |
| I'm only trying to get the D6DiceView working, but there will be multiple different dice views after this is working. | |
| struct DiceSelectView: View { | |
| [...] | |
| // Here is the array and initial value for each die | |
| @State var viewArray = [] as [Any] | |
| @State var initialRolledValue = "1" | |
| [...] | |
| // Here is where the views are displayed | |
| LazyHGrid (rows: selectedDiceColumns) { | |
| ForEach(0..<viewArray.count, id: \.self) { index in | |
| if self.viewArray[index] is D4DiceView { | |
| D4DiceView() | |
| .onTapGesture { | |
| withAnimation(.spring()) { | |
| self.viewArray.remove(at:index) | |
| print("D4 tapped") | |
| } | |
| } | |
| } else { | |
| D6DiceView(rolledValue: $initialRolledValue) | |
| .onTapGesture { | |
| withAnimation(.spring()) { | |
| self.viewArray.remove(at:index) | |
| print("D6 tapped") | |
| } | |
| } | |
| } | |
| } | |
| } | |
| [...] | |
| //Here is the button that is pressed to trigger the dice roll | |
| VStack { | |
| Button(action: rollButtonPressed, label: { | |
| Text("Roll Dice") | |
| .foregroundColor(.white) | |
| .padding() | |
| .frame(width: 120, height: 40) | |
| .background(Color(.systemIndigo)) | |
| .cornerRadius(10) | |
| }) | |
| //Here is the function that actions on the button press, "rolling" just the D6DiceViews | |
| func rollButtonPressed() { | |
| let d6Array = viewArray.compactMap {$0 as? D6DiceView} | |
| d6Array.forEach { item in | |
| item.d6rollButtonPressed() | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd try something like, in the
rollButtonPressed()functionInstead of putting that inside the view. Then for other dice types I'd add an enum to the view that you can switch through for generating different ranges.
Another idea would be to have an array of Ints in the main view. So when you initialize the view, set the binding to the correct index of that array, then generate values and update the array in the main View.