Reputation: 554
I am trying to show a certain medal in my scene depending on what your high score is based on the goal of the level.
// Get Medal Colour
if levelHighscore < goalScore {
scoreMedal = SKSpriteNode(imageNamed: "noMedal")
} else if levelHighscore == goalScore {
scoreMedal = SKSpriteNode(imageNamed: "bronzeMedal")
} else if levelHighscore > goalScore {
scoreMedal = SKSpriteNode(imageNamed: "silverMedal")
}
At the moment i get the high score and compare it to the goal - if it is less than the goal show noMedal image. If it equals the goal show a bronzeMedal, If the high score is 5 more than the goal show silverMedal and if the high score is 10 higher than the goal show a goldMedal.
Been on this for a while trying all different bits and bobs and for some reason it works in the setup above but when i write
// Get Medal Colour
if levelHighscore == goalScore+5 {
scoreMedal = SKSpriteNode(imageNamed: "silverMedal")
}
it shows nothing.
Upvotes: 0
Views: 124
Reputation:
There’s some great answers here but I’d choose to do it a little differently.
First of all, a series of concrete values lends itself to using an enum
:
/// Raw values are the minimum for each Medal type.
/// For .none the raw value indicates any negative value
enum Medal: Int {
case none = -1
case bronze = 0
case silver = 5
case gold = 10
}
Then we add an initializer that takes a score and a goal. We also add an image
property that returns an SKSpriteNode
:
extension Medal {
init(score: Int, goal: Int) {
switch score - goal {
case Medal.bronze.rawValue ..< Medal.silver.rawValue: self = .bronze
case Medal.silver.rawValue ..< Medal.gold.rawValue : self = .silver
case Medal.gold.rawValue ..< Int.max : self = .gold
default : self = .none
}
}
var image: SKSpriteNode {
switch self {
case .none : return SKSpriteNode(imageNamed: "noMedal")
case .bronze: return SKSpriteNode(imageNamed: "bronzeMedal")
case .silver: return SKSpriteNode(imageNamed: "silverMedal")
case .gold : return SKSpriteNode(imageNamed: "goldMedal")
}
}
}
Then you can simply do this to get the image:
let image = Medal(score: 14, goal: 10).image
or
let image = Medal.bronze.image
Upvotes: 0
Reputation: 42598
Depending on your taste, I find this a bit easier to read and understand.
switch levelHighscore - goalScore {
case 0 ..< 5: scoreMedal = SKSpriteNode(imageNamed: "bronzeMedal")
case 5 ..< 10: scoreMedal = SKSpriteNode(imageNamed: "silverMedal")
case 10 ... .max: scoreMedal = SKSpriteNode(imageNamed: "goldMedal")
default: scoreMedal = SKSpriteNode(imageNamed: "noMedal")
}
However, @rmaddy's answer is excellent.
Upvotes: 5
Reputation: 318854
You need your comparisons to be like this:
if levelHighscore < goalScore {
scoreMedal = SKSpriteNode(imageNamed: "noMedal")
} else if levelHighscore >= goalScore + 10 {
scoreMedal = SKSpriteNode(imageNamed: "goldMedal")
} else if levelHighscore >= goalScore + 5 {
scoreMedal = SKSpriteNode(imageNamed: "silverMedal")
} else {
scoreMedal = SKSpriteNode(imageNamed: "bronzeMedal")
}
The important part is to check for the higher values first, then the lower values.
To make the logic clearer, you can format the code as follows:
if levelHighscore < goalScore {
scoreMedal = SKSpriteNode(imageNamed: "noMedal")
} else {
// We now know the score is greater or equal to goal
// First see if it's hight enough for gold
if levelHighscore >= goalScore + 10 {
scoreMedal = SKSpriteNode(imageNamed: "goldMedal")
} else {
// Nope, maybe it's silver
if levelHighscore >= goalScore + 5 {
// Yes, it's 5 or more but less than 10 over so it's silver
scoreMedal = SKSpriteNode(imageNamed: "silverMedal")
} else {
// No, it's less than 5 over leaving bronze
scoreMedal = SKSpriteNode(imageNamed: "bronzeMedal")
}
}
}
Upvotes: 2