我想用手指旋转circleWithArrow并且总是知道这个圆圈上的点的位置。
我尝试了不同的方法。目前我认为这是一个纯粹的数学...... />我有以下代码:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = self.atPoint(location)
if let name = touchedNode.name {
if name == "circleWithArrow" {
let dx = location.x - touchedNode.position.x
let dy = location.y - touchedNode.position.y
let angle = atan2(dy, dx)
var deltaAngle = angle - startingAngle
if abs(deltaAngle) > CGFloat(M_PI) {
if (deltaAngle > 0) {
deltaAngle = deltaAngle - CGFloat(2*M_PI)
} else {
deltaAngle = deltaAngle + CGFloat(2*M_PI)
}
}
let dt = CGFloat(touch.timestamp - startingTime)
let actionRotate = SKAction.rotate(byAngle: deltaAngle, duration: TimeInterval(dt))
touchedNode.run(actionRotate)
// Update angle and time
startingAngle = angle
startingTime = touch.timestamp
if let spot = self.childNode(withName: "spot") as? SKSpriteNode {
let factor = 308 / 2 * sin(angle) //circleWithArrow.size.width == 308
spot.position.x = (self.defaultSpotPosition?.x)! + factor
print("zPosition: \(touchedNode.zPosition)") //always: zPosition: 0.0
}
}
}}
我想将箭头末端的位置x与某个位置的posiion.x链接起来。
我不明白如何同步它们。
箭头的结尾圆圈只是SKSpriteNode上的一个点。
我想旋转“circleWithArrow”并将其用作现场位置的控制器。
我想如果我能知道“circleWithArrow”的zPosition我可以计算箭头结束的x(圆圈上的点),但我发现SKAction.rotate对节点的zPosition没有影响。