Diljot Jawanda Diljot Jawanda - 1 year ago 76
Swift Question

Swift SpriteNode Initial Angle

I'm trying to set the initial angle of my sprite to be 90 degrees. I don't want to call an action, is there a way of setting the angle parameter of a sprite as you do with the position?

Answer Source


You can use the zRotation property of SKNode which accept radiants

Degrees to Radiants

There's a useful extension for that

extension Int {
    var degreesToRadians: Double { return Double(self) * M_PI / 180 }
    var radiansToDegrees: Double { return Double(self) * 180 / M_PI }

sprite.zRotation = CGFloat(90.degreesToRadians)

enter image description here

From the Api Doc

The Euler rotation about the z axis (in radians).

The default value is 0.0, which indicates no rotation. A positive value indicates a counterclockwise rotation. When the coordinate system is rotated, it affects the node and its descendants. The rotation affects the node’s frame property, hit testing, rendering, and other similar characteristics.

The Yoshi's Island image comes form here.