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

查看:87
本文介绍了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.

实施 <你的 SKScene 子类中的code> didChangeSize: ,并将你想要的任何逻辑放在你的节点上。

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天全站免登陆