【发布时间】:2016-01-25 11:44:12
【问题描述】:
所以我试图从我的分数类中调用一个函数,该函数会将分数加一:
Score.addOneToScore(1)
但我最终得到了这个错误:
无法将“Int”类型的值转换为预期的参数类型“Score”
我很确定我忽略了一些东西,但我似乎看不到它。
这是我的分数等级:
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)"
}
}
【问题讨论】:
-
func addOneToScore()在定义中不接受任何参数。但你通过了它 1? -
如果我取出一个并在没有它的情况下调用它,那么它只会给我另一个错误,说它在调用中缺少参数 #1 的参数,我也无法真正弄清楚为什么。
-
@Astrum 因为你不应该使用它(就像它是一个静态函数一样)。 addOneToScore() 是一个实例方法。看看 Greg 的回答,看看你应该如何使用你的代码。
标签: swift function class sprite-kit uilabel