swift - Attaching value to array isn't impossible, how to add a value to a button/array/image? -
i started coding, don't know things. want make simple game. explain shortly: when user pressed "click start" game button, 7 out of 52 cards in card deck revealed. did creating several buttons hold image of of card. when user clicks button, 7 cards opened random card.
first off, created array add photo names view controller, this:
var masterallcards = ["2_of_clubs", "2_of_spades", "2_of_diamonds", "2_of_hearts", "3_of_clubs", "3_of_spades", "3_of_diamonds", "3_of_hearts", "4_of_clubs", "4_of_spades", "4_of_diamonds", "4_of_hearts", "5_of_clubs", "5_of_spades", "5_of_diamonds", "5_of_hearts", "6_of_clubs", "6_of_spades", "6_of_diamonds", "6_of_hearts", "7_of_clubs", "7_of_spades","7_of_diamonds","7_of_hearts", "8_of_clubs", "8_of_spades", "8_of_diamonds", "8_of_hearts", "9_of_clubs", "9_of_spades", "9_of_diamonds", "9_of_hearts", "10_of_clubs", "10_of_spades", "10_of_diamonds", "10_of_hearts", "jack_of_clubs", "jack_of_spades", "jack_of_diamonds", "jack_of_hearts", "queen_of_clubs", "queen_of_spades", "queen_of_diamonds", "queen_of_hearts", "king_of_clubs", "king_of_spades", "king_of_diamonds", "king_of_hearts", "ace_of_clubs", "ace_of_spades", "ace_of_diamonds", "ace_of_hearts"]
than created ibaction outlet view controller, click start button followed:
@ibaction func testt(sender: uibutton) { thegobutton.enabled = false thegobutton.hidden = true slotcard1.setimage(uiimage(named: randomcardpicker("cardslot1")), forstate: uicontrolstate.normal) playablecard1.setimage(uiimage(named: randomcardpicker("playablecard1")), forstate: uicontrolstate.normal) playablecard2.setimage(uiimage(named: randomcardpicker("playablecard2")), forstate: uicontrolstate.normal) playablecard3.setimage(uiimage(named: randomcardpicker("playablecard3")), forstate: uicontrolstate.normal) playablecard4.setimage(uiimage(named: randomcardpicker("playablecard4")), forstate: uicontrolstate.normal) playablecard5.setimage(uiimage(named: randomcardpicker("playablecard5")), forstate: uicontrolstate.normal) playablecard6.setimage(uiimage(named: randomcardpicker("playablecard6")), forstate: uicontrolstate.normal) playablecard7.setimage(uiimage(named: randomcardpicker("playablecard7")), forstate: uicontrolstate.normal) //sender.setimage(uiimage(named: randomcardpicker("cardslot1")), forstate: uicontrolstate.normal) }
my function "randomcardpicker" followed:
func randomcardpicker (whatcarduplay: string) -> string { let randomindex = int(arc4random_uniform(uint32(masterallcards.count))) let randomcard = allcards[randomindex] let randomvalueofthecard = allvalues[randomindex] masterallcards.removeatindex(randomindex) allvalues.removeatindex(randomindex) storehereallvalues.append(randomvalueofthecard) storehereallcards.append(randomcard) return randomcard }
as can see added lot of random things, because can not figure out how put addition value array, int. button change image correctly, button/image needs hold value can use later compare 2 values. read enums, not figure out if work @ code.
so question: how add value button, can use compare other buttons in future?
as writing this, wondering let value of image, title of button, can read value out easy. correct me if wrong!
a simple approach might set tag
property on each uibutton
determine 1 you're dealing with.
Comments
Post a Comment