David Gourde
David Gourde

Reputation: 3914

Spritekit node zRotation doesn't keep aspect ratio of node when child of another node

I am building a game and I would like to be able to rotate a node. I can rotate is perfectly fine if the parent of that node is self (The GameScene), but if I add the node as a child of another node that doesn't have the same width x height ratio, it looses it's aspect ratio.

I thought I could simply find the scene ratio and multiply the node's width by it after changing parent, and this works fine but... When I rotate the node (zRotation), it stretches again. The maximum stretch is when the node is rotated at 90 degrees, as the new parent node is a rectangle that is higher than larger.

I was wondering if there is a way to always keep the aspect ration intact even when rotating and change node parent (coordinate system)?

I simply add nodes to bigRectangle (a big rectangle on the GameScene). It looks stretched on the rectangle (it does not if I add it to the GameScene) so I change the ratio by doing:

let myRatio = self.frame.height / self.frame.width
myNode.xScale = 1
myNode.yScale = 1 * myRatio

This works but when the node is rotated (zRotation) it becomes stretched again...

I added pictures;

Before rotation, ratio is good

After rotation, ratio is broken

Upvotes: 2

Views: 392

Answers (1)

Stefan
Stefan

Reputation: 5451

You have to configure this on the scene itself:

scene.scaleMode = .AspectFill

Upvotes: 0

Related Questions