coding22
coding22

Reputation: 689

How do I make my node rotate at a slower duration?

I have this node and when I press and hold a button I want the node to rotate slower. I changed the duration parameter from 1 to 50 and it still rotates the node the same speed. What am I doing wrong?

let rotateRate = (SKAction.rotateByAngle(CGFloat(-M_PI_2), duration: 50.0))
let repeatRotate = SKAction.repeatActionForever(rotateRate)
heroNode.runAction(repeatRotate)

Upvotes: 1

Views: 319

Answers (2)

Knight0fDragon
Knight0fDragon

Reputation: 16837

I would recommend Whirlwind's option first, this should be used 99% of the time, but in a case where changing the speed is not an option, just apply another action of rotateBy in the opposite direction at a smaller interval.

let rotateRate = (SKAction.rotateByAngle(CGFloat(-M_PI_2), duration: 50.0))
let repeatRotate = SKAction.repeatActionForever(rotateRate)
heroNode.runAction(repeatRotate)


...
func slowDown()
{
    let rotateRate = (SKAction.rotateByAngle(CGFloat(M_PI_4), duration: 50.0))
    let repeatRotate = SKAction.repeatActionForever(rotateRate)
    heroNode.runAction(repeatRotate, forKey:"slowdown")

}

func removeSlowDown()
{
    heroNode.removeActionForKey("slowdown")
}

Upvotes: 2

Whirlwind
Whirlwind

Reputation: 13665

Once you create an action, you can't modify its duration parameter... So you can't affect on speed of an action in the way you are expecting. But you have a few options:

  • to re-create the action (you probably want to run an action with key for this)

  • to change the speed of that action:

    if let action = node.actionForKey("aKey"){
    
       action.speed = 1.5
    }
    

Probably some more, but this will give an idea what is going on.

Upvotes: 3

Related Questions