swift - remove matching images from two Arrays -
i'm making snap game i'm trying remove 2 images array don't reappear again when first random card , second random card match. there way can this? thanks
var cardnamesarray: [string] = ["sad", "sleepy","angry"] var cardnamesarray2: [string] = ["triste", "cansado","enfadado"]
generates random card first array
firstrandomnumber = int (arc4random_uniform(3)) let firstcardstring:string = self.cardnamesarray[firstrandomnumber] self.firstcardimageview.image = uiimage(named: firstcardstring)
generates random card second array
secondrandomnumber = int (arc4random_uniform(3)) let secondcardstring:string = self.cardnamesarray2[secondrandomnumber] self.secondcardimageview.image = uiimage(named: secondcardstring)
if match player score 1 point
@ibaction func snapbuttontapped(sender: uibutton) { if firstrandomnumber == secondrandomnumber { print("index match") self.playerscore += 1 self.playerscorelabel.text = string(self.playerscore }
why don't try remove images array itself. once random number matches, can that!
if firstrandomnumber == secondrandomnumber { print("index match") self.playerscore += 1 self.playerscorelabel.text = string(self.playerscore cardnamesarray.removeatindex(firstrandomnumber) cardnamesarray2.removeatindex(secondrandomnumber) if cardnamesarray.count == 0 && cardnamesarray2.count == 0{ print("user won") }else{ print("continue playing") } }
crash update:
replace below 2 lines in code mine
firstrandomnumber = int (arc4random_uniform(uint32(cardnamesarray.count))) secondrandomnumber = int(arc4random_uniform(uint32(cardnamesarray2.count)))
Comments
Post a Comment