【发布时间】:2016-11-26 22:42:20
【问题描述】:
我在屏幕上有三个相同类MovableBlock 的节点lineBlock。我想旋转有人在屏幕上触摸的lineBlock 节点。
我已经解决了这个问题,以便在 touchMoved 中移动正确的 lineBlock 节点:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
touch = touches.first!
positionInScene = self.touch?.location(in: self)
let previousPosition = self.touch?.previousLocation(in: self)
let translation = CGVector(dx: (positionInScene?.x)! - (previousPosition?.x)!, dy: (positionInScene?.y)! - (previousPosition?.y)!)
if touchedNode?.name?.contains("LineBlock") == true {
(touchedNode as! MovableBlock).selected = true
(touchedNode as! MovableBlock).parent!.parent!.run(SKAction.move(by: translation, duration: 0.0))
}
}
但我无法在我的 UIRotationRecognizer 函数中做同样的事情。在我的旋转函数中,它只是旋转第一个节点,而不管我触摸的是哪个 lineBlock(属于 MovableBlock 类):
func rotate(_ sender: UIRotationGestureRecognizer){
if lineBlock.selected == true {
lineBlock.run(SKAction.rotate(byAngle: (-(self.rotationRecognizer?.rotation)!*2), duration: 0.0))
rotationRecognizer?.rotation = 0
}
}
作为参考,这是我定义touchBegan的方式(在touchBegan中):
touches: Set<UITouch>, with event: UIEvent?) {
touch = touches.first!
positionInScene = self.touch?.location(in: self)
touchedNode = self.atPoint(positionInScene!)
【问题讨论】:
-
很想使用touchNode,但touchNode在touchBegan或touchMoved之外变成nil
标签: ios swift sprite-kit swift3