SpriteKit 支持多种设备方向 [英] SpriteKit support multiple device orientations

查看:25
本文介绍了SpriteKit 支持多种设备方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要支持所有方向的 SpriteKit 游戏.现在,当我更改方向时,节点不会保持其位置.我使用 SKSceneScaleModeResizeFill 进行缩放,因为它会保持正确的精灵大小.

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 SKSceneScaleModeResizeFill for scaling, because it will keep the right sprite size.

当我开始游戏时,玩家是这样定位在屏幕中间的:

When I start the game, the game player is positioned in the mid screen like this:

然后当我旋转设备时,位置变成这样:

Then when I rotate the device, the position becomes like this:

这是我的视图控制器代码:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    // Configure the view.
    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {

        // Create and configure the scene.
        SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
        scene.scaleMode = SKSceneScaleModeResizeFill;

        // Present the scene.
        [skView presentScene:scene];
    }
}

还有我的场景代码:

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        //Add spaceship in the center of the view
        SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship.png"];
        spaceship.position = CGPointMake(size.width/2, size.height/2);
        [spaceship setScale:.3];
        [self addChild:spaceship];

    }
    return self;
}

推荐答案

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

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.

这篇关于SpriteKit 支持多种设备方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆