I deal out some cards (images)
With an .onTapgesture (3 taps) I want to discard (remove from hand) a selected card. My code updates the array correctly by removing the selected card, the selected card is added to the discard pile array, but the selected card is still showing in the hand while the last card is now not showing.
I pressed 3 times on the 8 of diamonds. The last card (6 of spades) is not showing, but the 8 still is.
When I go back to my menu screen then choose to show the hand again, it then shows it correctly.
Here are the code blocks:
import SwiftUI
struct p1Hand: View {
@EnvironmentObject var cardPositions: CardPositions
@EnvironmentObject var p1cards: P1Cards
@EnvironmentObject var lprvars: LPRvars
var body: some View {
ZStack {
playerHandBackground()
ForEach(0..<p1cards.p1hand.count, id: \.self) { cardnum in cardView(showcard: p1cards.p1hand[cardnum], cardPos: cardPositions.dr1Pos[cardnum], index: cardnum)}
}
.navigationTitle("Player 1's Hand").font(.footnote)
}
}
import SwiftUI
struct cardView: View {
@EnvironmentObject var cardPositions: CardPositions
@EnvironmentObject var carddecks: CardDecks
@EnvironmentObject var p1cards: P1Cards
@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)")
carddecks.discarddeck.append(showcard)
print("before: \(p1cards.p1hand)")
p1cards.p1hand.remove(at: index)
print("after: \(p1cards.p1hand)")
})
Thanks for your advice!
Kelly