我有一个人物拍摄,我希望他只在用户触摸屏幕时拍摄,我不断收到此错误"' GameScene'没有名为' stimer' 这是代码
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
var stimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnShot"), userInfo: nil, repeats: true)
let location = touch.locationInNode(self)
player.position.x = location.x
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent: UIEvent){
for touch: AnyObject in touches{
if self.stimer.valid{
self.timer.invalidate()
self.timer = nil
}
}
}
答案 0 :(得分:1)
您的问题是,方法内部声明的任何变量或常量仅在该方法中可用。
我认为,如果您将 if (top >= array.length -1) {
stimer
作为touchesBegan
之外的var stimer:NSTimer!
宣布为touchesBegan
,那么就可以解决此问题,然后在{{1}内完成您的论证}}。然后,您可能不需要在计时器名称之前使用self.
。
此外,看起来您正在使一个不同的计时器失效而不是检查其有效性?这可能是另一个问题,我不知道你是否故意这样做。
答案 1 :(得分:0)
您应该在GameScene fromHexString()
方法或任何其他初始函数中启动计时器,如下所示:
init
然后你需要创建var gameLoop:NSTimer?
var hitByEnemy:Bool = false
// you need to implement this method otherwise Xcode will throw an error
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(size: CGSize) {
super.init(size: size)
self.gameLoop = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "gameLoopTimerTicked:", userInfo: nil, repeats: true)
}
在经过一定时间间隔后调用的方法:
NSTimer
在另一个函数中,您应该定义您的角色是否被敌人或任何其他应该停止计时器的事件击中。