我是 Kotlin 新手,我决定编写一个简单的文本游戏。当我尝试访问敌人的生成率(女巫无关)时,我总是收到错误:
Kotlin: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public operator fun MatchGroupCollection.get(name: String): MatchGroup? defined in kotlin.text
.get
我的 IDE 中变成红色。
这是我的敌人龙:
"dragon" to mapOf(
"spawnRate" to mapOf("hill" to 1.01),
"drops" to mapOf(
"dragon scale" to mapOf("quantity" to listOf(1,10)),
"dragon egg" to 0.01,
"dragon head" to 1.01,
),
"exp" to listOf(1, 4),
"dmg" to listOf(35,75),
),
我试图像这样访问spawnRate:
val dragonSpawnRate = mobs["dragon"]?.get("spawnRate")?.get("hill")
println("Dragon Spawn Rate: $dragonSpawnRate")
我尝试了更多的想法,但最终都出现了同样的错误。
val dragon = mobs["dragon"]?.get("spawnRate")
val dragonSpawnRate = dragon?.get("hill")
//-----------------------------------------------
val dragon = mobs["dragon"]?.get("spawnRate")
val dragonSpawnRate = dragon["hill"]
我还尝试了这个问题的一些解决方案: https://stackoverflow.com/questions/61660123/define-class-parameter-with-get-at-kotlin
但他们都没有帮助。我也尝试询问chat gpt,但也没有帮助。