I'm trying to make my spriteNode rotate over finger touch.
So far I can do it, but what I want is that my node have a "rotation speed".
So I calculate the length of the angle then set a different timing to rotate with it (if the arc is long, it will take time...).
Here's my code :
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
_isTouched = true
for touch in touches {
let location:CGVector = touch.locationInNode(self) - miner.position
miner.weaponRotation = location.angle() - CGFloat(M_PI_2)
}
}
var wantedRotation: CGFloat {
get { return _wantedRotation }
set(rotation) {
if rotation > CGFloat(M_PI) || rotation < -CGFloat(M_PI) {
_wantedRotation = CGFloat(M_PI) + rotation % CGFloat(M_PI)
}
else {
_wantedRotation = rotation
}
removeActionForKey("rotation")
let arc: CGFloat = CGFloat(abs(_wantedRotation - zRotation))
let shortestArc: CGFloat = min(arc, CGFloat(M_PI * 2.0) - arc)
runAction(SKAction.rotateToAngle(_wantedRotation, duration: NSTimeInterval(shortestArc / CGFloat(M_PI) * rotationSpeed), shortestUnitArc: true), withKey: "rotation")
}
}
The main problem is that adding several SKAction
to the node block the movement.
I would like to know what could be the solution ? If possible by using SKAction, since I would like to avoid doing an update on each frame to calculate the movement (but if it's the only solution...)
NOTE AFTER ANSWER
As I received answers, I read again the SpriteKit documentation and found this clear note :
When You Shouldn’t Use Actions
Although actions are efficient, there is a cost to creating and executing them. If you are making changes to a node’s properties in every frame of animation and those changes need to be recomputed in each frame, you are better off making the changes to the node directly and not using actions to do so. For more information on where you might do this in your game, see Advanced Scene Processing.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…