且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SpriteKit 支持多种设备方向

更新时间:2023-02-02 10:25:36

你的精灵确实在场景调整大小后保持它的位置 - 你可以从你的截图中看到它保持相同的水平和垂直距离从场景的左下角.问题是,在场景调整大小后,绝对 偏移量代表场景中不同的相对 位置.

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 可以自动调整场景大小,但场景调整大小后节点的相对位置不是它可以为您做的事情.对于如何以不同大小重新排列场景内容并没有正确答案",因为场景内容的排列是您的应用定义的.

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.

实现 didChangeSize: 在您的 SKScene 子类中,并在其中放置您想要移动节点的任何逻辑.

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;
    }
}

不过,根据场景中的内容和您的安排,您可能不希望这样.例如,如果您在游戏场景的每个角落都有 HUD 元素,您可能希望它们与角落有固定的偏移量,而不是场景大小的比例.

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.