1

So I started making a game and I wanted to use a progress bar as a health bar for many different things and I couldn't figure out why it wasn't updating at all. It did, however, update in the viewdidload function. Here is the code that I am using.

func updateHealthBars() {
    hullHealthBar.setProgress(hullHealth/hullHealthMax, animated: true)
    cannonsHealthBar.setProgress(cannonsHealth/cannonsHealthMax, animated: true)
    sailsHealthBar.setProgress(sailsHealth/sailsHealthMax, animated: true)
    crewHealthBar.setProgress(crewHealth/crewHealthMax, animated: true)

}

@IBAction func setSailButton(sender: AnyObject) {
    if hullHealth > 0 {
        hullHealth-=20
    }
    else if cannonsHealth > 0 {
        cannonsHealth-=20
    }
    else if sailsHealth > 0 {
        sailsHealth-=20
    }
    else if crewHealth > 0 {
        crewHealth-=20
    }
    updateHealthBars()
}

If anybody knows what I need to do to update their health when I press the button, I would be very thankful because I have been trying to do this for a while now without success. I am using XCode 6.

Dima
  • 23,484
  • 6
  • 56
  • 83
samando
  • 140
  • 1
  • 1
  • 10

2 Answers2

2

In Swift, when assigning numbers using literals without decimals, it uses Int. ProgressView requires floats. Try the following:

func updateHealthBars() {
    dispatch_async(dispatch_get_main_queue(), ^{
        hullHealthBar.setProgress(hullHealth/hullHealthMax, animated: true)
        cannonsHealthBar.setProgress(cannonsHealth/cannonsHealthMax, animated: true)
        sailsHealthBar.setProgress(sailsHealth/sailsHealthMax, animated: true)
        crewHealthBar.setProgress(crewHealth/crewHealthMax, animated: true)
    });   
}

@IBAction func setSailButton(sender: AnyObject) {
    if hullHealth > 0.0 {
        hullHealth-=20.0
    }
    else if cannonsHealth > 0.0 {
        cannonsHealth-=20.0
    }
    else if sailsHealth > 0.0 {
        sailsHealth-=20
    }
    else if crewHealth > 0.0 {
        crewHealth-=20.0
    }
    updateHealthBars()
}

Make sure your properties are also floats: var hullHealth: Float = 0 or var hullHealth = 0.0

bsarrazin
  • 3,990
  • 2
  • 18
  • 31
  • No that is not the problem. I tried that and it didn't fix it. The variables have been floats from the very beginning. They just aren't updating at all. – samando Mar 13 '15 at 14:08
  • check to see if `hullHealthBar.setProgress(10.0/2.0, animated: true)` works – bsarrazin Mar 13 '15 at 14:17
  • You can only set progress values from 0.0 to 1.0 that would have a value of 5.0. – samando Mar 13 '15 at 17:25
  • ah yes, I meant `hullHealthBar.setProgress(1.0/2.0, animated: true)` – bsarrazin Mar 13 '15 at 17:31
  • last test: `hullHealthBar.setProgress(0.5, animated: true)` if that doesn't work either `hullHealthBar` is nil or is not visible on the screen – bsarrazin Mar 13 '15 at 17:40
  • Nope it is visible and it is not equal to nil. I read somewhere something about it having to be on a different thread but I have no idea what that means or has to do with just updating it once every time I click a button. – samando Mar 13 '15 at 20:11
  • you have to do UI changes on the main thread, I updated my answer – bsarrazin Mar 13 '15 at 20:11
  • Ok so I did that and it didn't work again(I wasn't expecting it to, and then I mad a new project with only a button and a progress view and it worked fine so Noel's I am trying to figure out why mine aren't changing. Thanks for trying to help me so much though. – samando Mar 13 '15 at 20:35
0

I finally got this to work. So my original code worked. The actual problem was something to do with my button not being connected right or something even though everything else worked fine. I made a new button and put the same things in it and it worked fine. It was a problem with the button, not the thread or anything.

samando
  • 140
  • 1
  • 1
  • 10