2015-11-13 77 views
0

我在我的第一个场景的重复动作,我想为用户触摸屏幕的首次尽快停止。是否有可能在didMoveToView中检测到触摸?我不能使用touchesBegan,因为这是唯一的第一个触摸一个特殊情况,我不希望它重复每一个触摸。如何在didMoveToView(Swift,SpriteKit)中注册touch?

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 
} 

回答

0

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 
    } 
}