无法从其他 class 调用函数
Can't call function from other class
所以我正在尝试从我的分数 class 中调用一个函数,它将为分数加一:
Score.addOneToScore(1)
但我最终得到了这个错误:
cannot convert value of type 'Int' to expected argument type 'Score'
我很确定我忽略了一些东西,但我似乎看不到它。
这是我的分数class:
import Foundation
import SpriteKit
import UIKit
class Score: SKLabelNode {
var number = 0
init (num: Int) {
super.init()
fontColor = UIColor.whiteColor()
fontName = "Helvetica"
fontSize = 75.0
number = num
text = "\(num)"
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addOneToScore() {
number++
text = "\(number)"
}
}
你犯的错误很少:
你正在尝试打电话
addOneToScore() 作为 class(静态)函数,但它是一个实例函数,您正在传递参数但函数不接受参数,试试这个:
let score = Score(num: 0) // Initialise score with the value of 1
score.addOneToScore() // add one to the score
print("\(score.number)") // print the value (1)
所以我正在尝试从我的分数 class 中调用一个函数,它将为分数加一:
Score.addOneToScore(1)
但我最终得到了这个错误:
cannot convert value of type 'Int' to expected argument type 'Score'
我很确定我忽略了一些东西,但我似乎看不到它。
这是我的分数class:
import Foundation
import SpriteKit
import UIKit
class Score: SKLabelNode {
var number = 0
init (num: Int) {
super.init()
fontColor = UIColor.whiteColor()
fontName = "Helvetica"
fontSize = 75.0
number = num
text = "\(num)"
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addOneToScore() {
number++
text = "\(number)"
}
}
你犯的错误很少: 你正在尝试打电话 addOneToScore() 作为 class(静态)函数,但它是一个实例函数,您正在传递参数但函数不接受参数,试试这个:
let score = Score(num: 0) // Initialise score with the value of 1
score.addOneToScore() // add one to the score
print("\(score.number)") // print the value (1)