Chris, sorry got tied up on non-swift activities. I have spent some time culling out just the code of my specific problem. I have a lot of images in my Assets that represent the shuffler and the cards. I am not sure how to send the Assets.
So when I run this code set, i shuffle the cards and it assigns cards to an array (player1cards). Then when I try to ādiscardā by tapping a card 3 times it works for removing from the player1cards array, but does not take off the screen. It does take off the last card showing, which it is not supposed to unless I discard it.
But here are all the code blocks:
// cardsApp.swift
// cards
import SwiftUI
@main
struct cardsApp: App {
@StateObject private var decks = Decks()
@StateObject private var player1cards = Player1Cards()
@StateObject private var cardlocations = CardLocations()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(decks)
.environmentObject(player1cards)
.environmentObject(cardlocations)
}
}
}
ContentView file:
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack{Color.cyan.ignoresSafeArea()
NavigationStack {
List() {
Group {
NavigationLink(destination: shuffleAndDeal(), label: {Text("Shuffle & Deal >").bold().fontWeight(.heavy).foregroundColor(.black)}).listRowBackground(Color(.systemTeal))
NavigationLink(destination: player1hand(), label: {Text("Player 1's Hand >").bold().fontWeight(.heavy).foregroundColor(.black)}).listRowBackground(Color(.systemTeal))
}
}
.navigationTitle("Menu")
.listStyle(.automatic)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// player1hand.swift
// cards
import SwiftUI
class Decks: ObservableObject {
@Published var twodecks: [String] = ["2_of_spades", "3_of_spades", "4_of_spades", "5_of_spades", "6_of_spades", "7_of_spades", "8_of_spades", "9_of_spades", "10_of_spades", "jack_of_spades", "queen_of_spades", "king_of_spades", "ace_of_spades", "2_of_hearts", "3_of_hearts", "4_of_hearts", "5_of_hearts", "6_of_hearts", "7_of_hearts", "8_of_hearts", "9_of_hearts", "10_of_hearts", "jack_of_hearts", "queen_of_hearts", "king_of_hearts", "ace_of_hearts"]
@Published var discarddeck: [String] = ["2_of_clubs"]
}
class Player1Cards: ObservableObject {
@Published var p1hand: [String] = []
}
class CardLocations: ObservableObject {
@Published var dr1Pos: [CGPoint] = [CGPoint(x: 30, y: 75), CGPoint(x: 50, y: 75), CGPoint(x: 70, y: 75), CGPoint(x: 90, y: 75), CGPoint(x: 110, y: 75), CGPoint(x: 130, y: 75), CGPoint(x: 150, y: 75), CGPoint(x: 170, y: 75), CGPoint(x: 190, y: 75), CGPoint(x: 210, y: 75), CGPoint(x: 230, y: 75), CGPoint(x: 250, y: 75), CGPoint(x: 270, y: 75), CGPoint(x: 290, y: 75), CGPoint(x: 310, y: 75), CGPoint(x: 330, y: 75), CGPoint(x: 350, y: 75), CGPoint(x: 370, y: 75)]
}
struct shuffleAndDeal: View {
@EnvironmentObject var decks: Decks
@EnvironmentObject var player1cards: Player1Cards
@EnvironmentObject var cardlocations: CardLocations
var body: some View {
Button(action: {
decks.twodecks.shuffle()
var counter = 0
while counter < 10 {
player1cards.p1hand.append(decks.twodecks[counter])
counter += 1
}
}, label: { VStack {
Spacer()
Image("Shuffle").resizable().frame(width: 150, height: 150)
Text("Shuffle!").font(.largeTitle).foregroundColor(.orange)
}})
}
}
struct player1hand: View {
@EnvironmentObject var decks: Decks
@EnvironmentObject var player1cards: Player1Cards
@EnvironmentObject var cardlocations: CardLocations
var body: some View {
ZStack {
ForEach(0..<player1cards.p1hand.count, id: \.self) { cardnum in cardViewV2(showcard: player1cards.p1hand[cardnum], cardPos: cardlocations.dr1Pos[cardnum], index: cardnum)}
}
.navigationTitle("Player 1's Hand").font(.footnote)
}
}
struct cardViewV2: View {
@EnvironmentObject var cardpositions: CardLocations
@EnvironmentObject var decks: Decks
@EnvironmentObject var player1cards: Player1Cards
@GestureState private var pressing: Bool = false
@State private var expand: Bool = false
@State var showcard: String
@State var cardPos: CGPoint
@State var index: Int
// arguments passed in: (cardName, cardPos)
var body: some View {
Image(showcard).resizable().frame(width: 40, height: 60).position(cardPos)
// 3 taps -> discard: put on discard pile and remove from hand
.onTapGesture(count: 3, perform: {
print("discard card: \(showcard), index: \(index)")
decks.discarddeck.append(showcard)
print("before: \(player1cards.p1hand)")
player1cards.p1hand.remove(at: index)
print("after: \(player1cards.p1hand)")
})
}
}
struct player1hand_Previews: PreviewProvider {
static var previews: some View {
player1hand()
.environmentObject(Decks())
.environmentObject(CardLocations())
.environmentObject(Player1Cards())
}
}
I have pondered this for quite a while and appreciate yours or any others advice and suggestions!
Thanks,
Kelly