我在Swift中制作一个简单的SpriteKit游戏。我做了一个文件" EmemyAppear.swift"并把它放在一个类和一个函数。这就是这个文件中的内容:
import Foundation
class EnemyAppear {
var nowAppear = false
var waitToAppear = UInt32(0)
var appearInterval = UInt32(0)
init(nowAppear:Bool, waitToAppear:UInt32, appearInterval:UInt32) {
self.nowAppear = nowAppear
self.waitToAppear = waitToAppear
self.appearInterval = appearInterval
}
func shouldRun() -> Bool {
return self.appearInterval > self.waitToAppear
}
}
然后我在我的主要场景中制作了一本字典:
var enemyStatus:Dictionary<String,EnemyAppear> = [:]`
之后,我尝试联系EnemyAppear.swift中的变量和函数,如下所示:
func enemyRun() {
for(enemy, enemyAppear) in self.enemyStatus {
var thisPet = self.childNodeWithName(enemy)!
if enemyStatus.shouldRun() {
enemyStatus.waitToAppear = random()
enemyStatus.appearInterval = 0
enemyStatus.nowAppear = true
}
}
}
但Xcode告诉我例如Dictionary <String, EnemyAppear> doesn't have a member named 'shouldRun'
但确实如此。我做错了什么?
答案 0 :(得分:2)
if enemyStatus.shouldRun()
enemyStatus是一个Dictionary,而不是EnemyAppear,然后它没有名为'shouldRun'的成员。它应该是
if enemyAppear.shouldRun()