swiftsprite-kitskspritenodeskaction

is it okay to call a run SKAction method inside of the update function?


I am running the SKAction.rotate(to:) method on an SkspriteNode to animate the sprite's rotation. It works alright calling it whenever I need it to point towards a new location, however it rotates in a much more desirable way running it continuously inside of the update function. It seems to run fine like this but I want to avoid any performance backlash that my program might incur. The sprite is a moving object and it is not repeating the same rotation over and over. I don't want it to instantly rotate but rather it have a smooth rotate animation. Am I making an acceptable decision calling it to run the rotate action inside of the update function?

let scout = SKSpriteNode()

func findAngle(touchPos: CGPoint) -> CGFloat {
    let dy = touchPos.y - scout.position.y
    let dx = touchPos.x - scout.position.x
    let angle = atan2(dy, dx) - CGFloat(Double.pi / 2)
    
    return angle
}
func rotateToAngle(touchPos: CGPoint) {
    let speed = CGFloat(300)
    let duration = 1.0
    let rotateAction = SKAction.rotate(toAngle: findAngle(touchPos: touchPos), duration: duration, shortestUnitArc: true)
    scout.run(rotateAction)
}

Here is an example, I can call rotateToAngle inside touches began however I am getting better end result calling it inside update. The sprite is constantly moving. But is it okay for performance to repeatedly run an SKAction like this every 1/60th of a second?


Solution

  • Yes.

    There is no inherent issue running SKAction within update:()(or from a function called from update).

    Can you expand your question to include the code that is showing the problem?

    Also, is this within the simulator (on which performance can be variable) or on an actual device?