我在这里要做的是旋转一个SKSpriteNode围绕它的锚点,并有它的速度和方向匹配的潘手势。因此,如果我的盘手势顺时针围绕雪碧,那么精灵会顺时针旋转。
我的代码存在的问题是,它对雪碧下面的平底锅(从左到右/从右到左)很好,但当我尝试垂直平移时就一点都不起作用了,如果我把雪碧移到雪碧上面,它会使它以错误的方式旋转。
到目前为止我得到的是-
let windmill = SKSpriteNode(imageNamed: "Windmill")
override func didMoveToView(view: SKView) {
/* Setup gesture recognizers */
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
self.view?.addGestureRecognizer(panGestureRecognizer)
windmill.physicsBody = SKPhysicsBody(circleOfRadius: windmill.size.width)
windmill.physicsBody?.affectedByGravity = false
windmill.name = "Windmill"
windmill.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
self.addChild(windmill)
}
func handlePanGesture(recognizer: UIPanGestureRecognizer) {
if (recognizer.state == UIGestureRecognizerState.Changed)
{
pinwheel.physicsBody?.applyAngularImpulse(recognizer.velocityInView(self.view).x)
}
}我知道它没有用垂直平底锅旋转的原因,因为我只得到x值,所以我想我需要以某种方式组合它们。
我也尝试过使用applyImpulse:atPoint:,但是这会导致整个精灵被冲走。
发布于 2014-10-31 04:47:27
以下步骤将根据pan手势旋转节点:
这里有一个如何在Swift中实现这一点的例子..。
// Declare a variable to store touch location
var startingPoint = CGPointZero
// Pin the pinwheel to its parent
pinwheel.physicsBody?.pinned = true
// Optionally set the damping property to slow the wheel over time
pinwheel.physicsBody?.angularDamping = 0.25声明pan处理程序方法
func handlePanGesture(recognizer: UIPanGestureRecognizer) {
if (recognizer.state == UIGestureRecognizerState.Began) {
var location = recognizer.locationInView(self.view)
location = self.convertPointFromView(location)
let dx = location.x - pinwheel.position.x;
let dy = location.y - pinwheel.position.y;
// Save vector from node to touch location
startingPoint = CGPointMake(dx, dy)
}
else if (recognizer.state == UIGestureRecognizerState.Ended)
{
var location = recognizer.locationInView(self.view)
location = self.convertPointFromView(location)
var dx = location.x - pinwheel.position.x;
var dy = location.y - pinwheel.position.y;
// Determine the direction to spin the node
let direction = sign(startingPoint.x * dy - startingPoint.y * dx);
dx = recognizer.velocityInView(self.view).x
dy = recognizer.velocityInView(self.view).y
// Determine how fast to spin the node. Optionally, scale the speed
let speed = sqrt(dx*dx + dy*dy) * 0.25
// Apply angular impulse
pinwheel.physicsBody?.applyAngularImpulse(speed * direction)
}
}https://stackoverflow.com/questions/26591500
复制相似问题