每当我尝试使用self
目标使用NSTimer
时,都会弹出一个错误,说
为了安全起见,参数类型"NSObject-> () -> GameScene"不符合预期的类型"AnyObject"。
我还尝试添加AnyObject
而不是self
:
class GameScene: SKScene {
var point = SKSpriteNode(imageNamed: "Point")
override func didMoveToView(view: SKView) {
}
var timer = NSTimer.scheduledTimerWithTimeInterval(0.0001, target: self, selector: ("stroke"), userInfo: nil, repeats: true)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
func stroke(){
point.position = CGPoint(x: location.x, y: location.y)
self.addChild(point)
}
}
}
}
我认为问题是您在变量声明期间调用函数,并且很可能尚未创建自我或对象,并且这就是您收到错误的原因。
将函数调用移动到构造函数或初始化期间调用的函数中,它应该没问题。
var timer : NSTimer!
...
...
func someFunc()
{
timer = NSTimer.scheduledTimerWithTimeInterval(0.0001, target: self, selector: ("stroke"), userInfo: nil, repeats: true)
}