我在第一个场景中有一个重复 Action ,我想在用户第一次触摸屏幕时立即停止。是否可以在 didMoveToView
中检测触摸?我无法使用 touchesBegan
,因为这是仅第一次触摸的特殊情况,我不希望每次触摸都重复它。
override func didMoveToView(view: SKView) {
triangle.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
self.addChild(triangle)
triangle.runAction(SKAction.repeatActionForever(rotateAction))
//->This is where I need to detect a touch
}
最佳答案
在 didMoveToView
期间——尤其是当您的应用程序启动时第一次调用它时——目前还不可能有当前触摸事件,因为您没有运行来响应主事件循环。如果您想处理触摸,touchesBegan
就是执行该操作的地方。
如果您只想在收到的第一个触摸事件上执行某些操作,则只需跟踪触摸事件是否是第一个即可。例如:
var touchedBefore = false
override func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?) {
if !touchedBefore {
touchedBefore = true
// do your first-touch business
} else {
// do your touch handling for other times
}
}
关于ios - 如何在didMoveToView(Swift,SpriteKit)中注册触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33698725/