0

I am developing a game for iOS 13 using Xcode 11 and Swift 5. (I'm a beginner.) I have 16 buttons on the screen, and I want to change a button's image and isEnabled setting randomly. I have this function that is called:

func startSequence()
{
    timeUntilChange = Int.random(in: 1...2)
    nextMole = Int.random(in: 0...15)
    timeMoleShows = Double.random(in: 0.5...1.5)
    timer = Timer.scheduledTimer(timeInterval: TimeInterval(timeUntilChange), target: self, selector: #selector(showMole), userInfo: nil, repeats: false)
}`

Then I have this:

@objc func showMole()
{
    if gameInSession == true
    {      
        moleImage_0.setBackgroundImage(UIImage(named:"mole_t.png"), for: [])
        moleImage_0.isEnabled = true
    }
}

So where I have moleImage_0 I want the 0 to be the randomly generated number held in the variable "nextMole".

I did search here and found some things about arrays and dictionaries. I tried an array, but couldn't figure it out.

Is what I'm trying to do possible?

1
  • keep working on the array idea. it will be like moleImage[randomIndex].setBackgroundImage(....) Commented Feb 26, 2020 at 2:14

3 Answers 3

1

attach all buttons to outlet collections and update your showMole with following.

@IBOutlet var allButtons: [UIButton]!
@objc func showMole()
{
    if gameInSession == true
    {
        allButtons[nextMole].setBackgroundImage(UIImage(named:"mole_t.png"), for: [])
        allButtons[nextMole].isEnabled = true
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

To expand on what John said, you could store all your mole buttons in an array defined like so (assuming your buttons are instances/subclasses of UIButton):

var moleButtons = [UIButton]()

Then you could even make your nextMole value a computed property like so:

var nextMoleIndex: Int {
    return Int.random(in: 0 ..< moleButtons.count)
}

Finally, your showMole function can be updated to reference this array when updating the random button:

@objc func showMole() {
    if gameInSession {
        moleButtons[nextMoleIndex].setBackgroundImage(UIImage(named:"mole_t.png"), for: .normal)
        moleButtons[nextMoleIndex].isEnabled = true
    }
}

Hope this helps!

Comments

0

I like the answer mwahlig provided, but in case you are not sure how to get the 16 buttons into the moleButtons array, you'll need to add each one to it like this somewhere in your setup code:

moleButtons.append(button1)
moleButtons.append(button2)
moleButtons.append(button3)
// etc. for buttons 4 through 16

Also, as to your specific question about being able to randomly generate the "0" part of the variable name "moleImage_0" - you can't do that in Swift; constant and variable names are fixed when you declare and/or define them.

Hope that helps you out!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.