我正在尝试创建一个iOS游戏,我已经拥有它,以便在触摸和移动项目时将其拖动。拖动时,鼠标会捕捉到精灵的中心。我怎么能这样做才不会发生这种情况?
Here是正在发生的事情的一个例子。
以下是处理触摸输入的功能
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var papers = 0
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
let touchedNode = nodeAtPoint(location)
if ((touchedNode.name?.contains("paper")) != nil) {
touchedNode.position = location
touchedNode.position.x = CGRectGetMidX(self.frame)
}
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let touchedNode = nodeAtPoint(location)
if ((touchedNode.name?.contains("paper")) != nil) {
touchedNode.position = location
touchedNode.position.x = CGRectGetMidX(self.frame)
}
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let touchedNode = nodeAtPoint(location)
if ((touchedNode.name?.contains("paper")) != nil) {
touchedNode.zPosition = 0
touchedNode.position.x = CGRectGetMidX(self.frame)
}
}
}
P.S。 contains
是String类的扩展,用于检查子字符串是否在字符串
提前致谢!