有更好的方法来使用这个 Switch 函数(Swift-Spritekit 项目)

There is a better way to use this Switch func (Swift-Spritekit project)

switch meter {
        case 0...2499:
            bgImage = "backgroundDay"
        case 2500...4999:
            bgImage = "backgroundNight"
        case 5000...7499:
            bgImage = "backgroundDay"
        case 7500...9999:
            bgImage = "backgroundNight1"
        case 10000...12499:
            bgImage = "backgroundDay1"
        case 12500...14999:
            bgImage = "backgroundNight2"
        case 15000...17499:
            bgImage = "backgroundDay2"
        case 17500...19999:
            bgImage = "backgroundNight3"
        case 20000...22499:
            bgImage = "backgroundDay3"
        case 22500...24999:
            bgImage = "backgroundNight4"
        case 25000...27499:
            bgImage = "backgroundDay4"
        case 27500...29999:
            bgImage = "backgroundNight5"
        case 30000...32499:
            bgImage = "backgroundDay5"
        case 32500...34999:
            bgImage = "backgroundNight6"
        case 35000...37499:
            bgImage = "backgroundDay6"
        case 37500...39999:
            bgImage = "backgroundNight7"
        case 40000...42499:
            bgImage = "backgroundDay7"
        case 42500...44999:
            bgImage = "backgroundNight8"
        case 45000...47499:
            bgImage = "backgroundDay8"
        case 47500...49999:
            bgImage = "backgroundNight9"
        case 50000...52499:
            bgImage = "backgroundDay9"
        case 52500...54999:
            bgImage = "backgroundNight10"
        case 55000...57499:
            bgImage = "backgroundDay10"
        case 57500...59999:
            bgImage = "backgroundNight11"
        case 60000...62499:
            bgImage = "backgroundDay11"
        case 62500...64999:
            bgImage = "backgroundNight12"
        case 65000...67499:
            bgImage = "backgroundDay12"
        case 67500...69999:
            bgImage = "backgroundNight13"
        case 70000...72499:
            bgImage = "backgroundDay13"

等...直到案例 150000

有没有更好的方法来做这样的事情? 我需要根据玩家到目前为止 运行

的米数更改已加载视图中的背景图像

您的值似乎以 2500 的增量均匀分布。如果 meterInt,您可以将所有 String 放入一个数组中,然后计算索引:

let bgImages = ["backgroundDay", "backgroundNight", "backgroundDay"...]
bgImage = bgImages[meter/2500]

方法 2:利用字符串的重复性质并计算它们:

let index = meter/2500

switch index {
case 0, 2:
    bgImage = "backgroundDay"
case 1:
    bgImage = "backgroundNight"
default:
    let i = index - 1
    if i & 1 == 0 {
        bgImage = "backgroundNight\(i/2)"
    } else {
        bgImage = "backgroundDay\(i/2)"
    }
}