SpriteKit support multiple device orientations

前端 未结 3 1187
旧巷少年郎
旧巷少年郎 2021-01-06 04:48

I have a SpriteKit game which I want to support all orientations. Right now when I change the orientation, the node doesn\'t keep its position. I use the SKSceneScaleM

3条回答
  •  情话喂你
    2021-01-06 05:26

    Your sprite does keep its position after the scene resizes — you can see from your screenshots that it keeps the same horizontal and vertical distance from the lower left corner of the scene. The catch is that after the scene has resized, that absolute offset represents a different relative position in your scene.

    Sprite Kit can resize a scene automatically, but the relative positioning of nodes after a scene resize isn't something it can do for you. There's no "right answer" to how a scene's content should be rearranged at a different size, because the arrangement of scene content is something your app defines.

    Implement didChangeSize: in your SKScene subclass, and put whatever logic you want there for moving your nodes.

    For example, you could make it so nodes keep their positions as a relative proportion of the scene size using something like this:

    - (void)didChangeSize:(CGSize)oldSize {
        for (SKNode *node in self.children) {
            CGPoint newPosition;
            newPosition.x = node.position.x / oldSize.width * self.frame.size.width;
            newPosition.y = node.position.y / oldSize.height * self.frame.size.height;
            node.position = newPosition;
        }
    }
    

    Depending on what's in your scene and you you've arranged it, you probably don't want that, though. For example, if you have HUD elements in each corner of your game scene, you might want them at a fixed offset from the corners, not a proportion of the scene size.

提交回复
热议问题