带有SpriteKit的iOS通用设备应用程序,如何为所有视图缩放节点? [英] iOS Universal Device App with SpriteKit, how to scale nodes for all views?

查看:13
本文介绍了带有SpriteKit的iOS通用设备应用程序,如何为所有视图缩放节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让 landscape 应用程序 通用,以便精灵节点与运行应用程序的任何视图大小成比例地缩放.我想要一个完全编程的解决方案,因为我不喜欢 IB.

I want to make a landscape app to be universal, so that the sprite nodes scale proportionally to whatever view size is running the app. I'd like an entirely programmatic solution because I don't like the IB.

我的游戏非常简单,我不需要任何形式的滚动或缩放,因此整个游戏将始终呈现并占据整个视图.

My game is pretty simple, and I don't need scrolling or zooming of any kind, so the whole game will always be present and take up the entire view.

我正在寻找的可能是更改场景的大小以始终适合视图吗?如果是这样,您能否彻底解释一下,因为我已尝试更改视图控制器的这一部分

Is it possible that what I'm looking for is to change the size of the scene to always fit the view? If so, can you explain this thoroughly because I've tried changing this section of my view controller

    if let scene = GameScene(fileNamed:"GameScene")

做一个以size为参数的构造方法,但是Xcode不喜欢这样.

to be the constructor method that takes size as a parameter but Xcode doesn't like that.

我尝试过的事情

  1. 使用 self.view.bounds.width/height 的分数.这通常会使所有 iPhone 看起来都不错,但在 iPad 上会拉伸和倾斜节点以及视图周围的边界框.
  2. 在所有四种类型中更改 scaleMode.我想保持良好的做法,并觉得 .AspectFill(默认)是我应该让我的应用程序使用的那个,但对建议持开放态度.笔记;我不希望在任何设备上出现黑边,只希望整个视图按比例显示/缩放.
  3. 应用程序化约束.现在我对此很陌生,并不完全理解约束,但是我什至从 RayWenderlich 看到的教程都没有谈论节点上的约束,所以我没有深入研究.
  4. 使用这样的方法在视图之间转换点.这实际上对于节点的点定位非常有效,如果可能的话,我希望这种方法能够解决,但是我仍然遇到节点大小的问题.此外,当我使用这种方法为 iPad 构建时,视图似乎以纵向开始,节点看起来很好,但随后我必须手动将其切换为横向,精灵和视图边界再次变得混乱.方法如下:

  1. Using fractions of self.view.bounds.width/height. This usually makes all iPhones look good, but on iPads stretches and skews nodes and the boundary box around thew view.
  2. Changing the scaleMode among all four types. I'd like to keep good practice and feel like .AspectFill (default) is the one I should make my app work with, but open to suggestions. Note; I don't want black edges on any device, just the entire view displayed/scaled proportionally.
  3. Applying programmatic constraints. Now I'm fairly new to this and don't understand constraints completely, but no tutorials I've seen even from RayWenderlich talk about constraints on nodes so I didn't delve to deep in this.
  4. Using a method like this to convert points among views. This actually worked pretty well for point positioning of nodes, and if possible I would like this method to work out, but then I still have the problem of sizes of nodes. Also when I build for iPad with this method the view seems to start off as portrait and the nodes look fine but then I have to manually switch it to landscape and the sprites and view boundaries once again get messed up. Here's the method:

func convert(point: CGPoint)->CGPoint {
    return self.view!.convertPoint(CGPoint(x: point.x, y:self.view!.frame.height-point.y), toScene:self)
}

  • 关于 RW 和互联网上其他任何地方的无数视频教程.

  • Countless vid tutorials on RW and everywhere else on internet.

    提前致谢!我很感激帮助.我知道这个话题很奇怪,因为很多人对此提出了问题,但每个人的情况似乎都不同,以至于一个解决方案并不适合所有人.

    Thanks in advance! I appreciate the help. I know this topic is weird because a lot of people ask questions about it but everyone's situation seems to be different enough that one solution doesn't fit all.

    推荐答案

    我最初尝试用 2 个游戏自己进行缩放,结果太疯狂了(场景大小 = 视图大小或场景缩放模式 = .ResizeFill).您必须调整所有设备的所有值,例如字体大小、精灵大小、脉冲等,它永远不会保持一致.

    I initially tried to do the scaling myself with 2 games and it was just madness (scene size = view size or scene scale mode = .ResizeFill). You have to adjust all values e.g font size, sprite sizes, impulses etc for all devices and it will never be consistent.

    所以你基本上有两个选择

    So you have 2 options basically

    1) 将场景大小设置为 1024X768(横向)或 768x1024(纵向).这是 Xcode 7 中的默认设置.

    1) Set scene size to 1024X768 (landscape) or 768x1024 (portrait). This was the default setting in Xcode 7.

    您通常只是在 iPad 上的顶部/底部(横向)或左侧/右侧(纵向)有/显示一些额外的背景,而这些背景在 iPhone 上会被裁剪.

    You than usually just have/show some extra background at the top/bottom (landscape) or left/right (portrait) on iPads which gets cropped on iPhones.

    在 iPad 上显示更多/在 iPhone 上裁剪的游戏示例:

    Examples of games that show more on iPads / crop on iPhones:

    Altos Adventure、Leos Fortune、Limbo、The Line Zen、Modern Combat 5.

    Altos Adventure, Leos Fortune, Limbo, The Line Zen, Modern Combat 5.

    2) Apple 将 xCode 8 中的默认场景大小更改为 iPhone 6/7(7501334-Portait、1337750-Landscape).此设置将裁剪您在 iPad 上的游戏.

    2) Apple changed the default scene size in xCode 8 to iPhone 6/7 (7501334-Portait, 1337750-Landscape). This setting will crop your game on iPads.

    在 iPad 上显示较少的游戏示例:

    Examples of games that show less on iPads:

    Lumino City,机器人独角兽攻击

    Lumino City, Robot Unicorn Attack

    这两个选项之间的选择取决于您,并且取决于您正在制作的游戏.我通常更喜欢使用选项 1 并在 iPad 上显示更多背景.

    Chosing between the 2 options is up to you and depends what game you are making. I usually prefer to use option 1 and show more background on iPads.

    无论场景大小,缩放模式通常最好保留默认设置 .aspectFill.

    Regardless of scene size scale mode is usually best left at the default setting of .aspectFill.

    要调整标签等特定的东西,你可以这样做

    To adjust specific things such as labels etc you can do it this way

     if UIDevice.current.userInterfaceIdiom == .pad {
       ...
     }
    

    您可以自己尝试场景缩放,创建一个新的 SpriteKit 示例游戏项目.在所有 iPhone 上运行,您会发现 HelloWorld 标签在所有设备上看起来都很完美.

    You can try the scene scaling yourself, create a new SpriteKit sample game project. Run on all iPhones and you will notice the HelloWorld label looks perfect on all devices.

    现在将默认设置更改为场景大小 = 帧或使用 .ResizeFill,HelloWorld 标签不再在所有设备上正确缩放.

    Now change the default settings to scene size = frame or use .ResizeFill, the HelloWorld label is not scaled properly anymore on all devices.

    作为旁注,这条线

     if let scene = GameScene(fileNamed: "GameScene")
    

    引用 GameScene.sks 文件.您说您以编程方式完成所有操作,因此您可以删除 GameScene.sks 文件并将行更改为

    references the GameScene.sks file. You said you do everything programatically, therefore you can probably delete the GameScene.sks file and change the line to

     let skView = view as! SKView!
     let scene = GameScene(size: CGSize(width: 1024, height: 768)) // 768 x 1024 if portrait
    

    更新:

    我现在使用稍微不同的变体,因为我在将我的游戏适应 iPhoneX 时遇到了问题.我将场景大小设置为 1334x750,并使用宽高比作为缩放模式.如果需要,我会运行一些代码来删除黑条,例如在 iPad 或 iPhone X 上.它基于这篇很棒的文章(链接不再有效).

    I am now using a slightly different variant as I had problems adapting my game to iPhoneX. I set scene size to 1334x750 and use aspect fit as scale mode. I than run some code to remove the black bars if needed e.g. on iPads or iPhone X. It’s based on this great article (link no longer works).

    http://endlesswavesoftware.com/blog/spritekit-skscene-scalemode/

    这篇关于带有SpriteKit的iOS通用设备应用程序,如何为所有视图缩放节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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