invisibloom
invisibloom

Reputation: 105

SKSpriteNode zrotation M_PI is infuriating

so all i want to do is rotate an SKSPriteNode by 90 degrees. just that. It should be simple yet my first approach, assuming it would be degrees, turns the object in completely the wrong diection. so i head to google > stackoverflow. plenty of answers to do with this, okay so ill try using M_PI or some variation. nope. 'Double' is not convertible to 'CGFloat'. google again. "Try using skaction with blah blah" nope.

how difficult can it be to rotate a sprite? or am i insane

Upvotes: 4

Views: 4596

Answers (4)

0x141E
0x141E

Reputation: 12753

I'm assuming you're using Swift based on your "'Double' is not convertible..." comment.

The following rotates a sprite 90 degree counterclockwise:

sprite.runAction(SKAction.rotateByAngle(CGFloat(M_PI_2), duration: 1.0))

The following rotates a sprite 90 degree clockwise:

sprite.runAction(SKAction.rotateByAngle(CGFloat(-M_PI_2), duration: 1.0))

Upvotes: 2

Bjeld
Bjeld

Reputation: 41

Another option would be to create an extension for Int:

extension Int
{
    var deg2Rad : CGFloat
    {
        return CGFloat(self) * CGFloat(M_PI) / 180.0
    }
}

then use it like this:

sprite.runAction(SKAction.rotateByAngle(180.deg2Rad, duration: 1.0))

Makes it more human-readable.

Upvotes: 2

Mateus
Mateus

Reputation: 405

let threesixty:CGFloat = 360 * .pi / 180

Upvotes: 0

baskInEminence
baskInEminence

Reputation: 762

This seems to work fine for me. Understanding conversions between radians and degrees is important. http://en.wikipedia.org/wiki/Radian#Conversion_between_radians_and_degrees

sprite.zRotation = CGFloat(M_PI_2)

Upvotes: 3

Related Questions