SpriteKit support multiple device orientations

前端 未结 3 1186
旧巷少年郎
旧巷少年郎 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:19

    For Swift 3,

    override func didChangeSize(_ oldSize: CGSize) {
        for node in self.children{
            let newPosition = CGPoint(x:node.position.x / oldSize.width * self.frame.size.width,y:node.position.y / oldSize.height * self.frame.size.height)
            node.position = newPosition
        }
    }
    

    Thus we are able to use a constant and initialise it in one line. Then in node.position = newPosition we can set the new position.
    Also we are able to make use of the enhanced for loop leading to a much more elegant solution.

    0 讨论(0)
  • 2021-01-06 05:24

    I add I similar issue and found this question. I solved differently, using the viewWillTransitionToSize:withTransitionCoordinator:, as stated by @GOR here.

    I added the following code in my view controller (that manage the SKView and its SKScene)

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
    {
        //skView is my SKView object, with scaleMode SKSceneScaleModeResizeFill
        skView.frame = CGRectMake(0, 0, size.width, size.height);
    
        //currentScene is my SKScene object
        currentScene.size = skView.frame.size;
    
        //Then, as all the objects in my scene are children of a unique SKNode* background, I only relocate it 
        currentScene.background.position = CGPointMake(CGRectGetMidX(currentScene.frame), CGRectGetMidY(currentScene.frame));
    }
    

    and it works like a charm!

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题