简体   繁体   English

带有 SpriteKit 的 iOS 通用设备应用程序,如何为所有视图缩放节点?

[英]iOS Universal Device App with SpriteKit, how to scale nodes for all views?

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.我想要一个完全程序化的解决方案,因为我不喜欢 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")

to be the constructor method that takes size as a parameter but Xcode doesn't like that.成为将 size 作为参数的构造函数方法,但 Xcode 不喜欢那样。


Things I've tried我尝试过的事情

  1. Using fractions of self.view.bounds.width/height.使用 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.这通常会使所有 iPhone 看起来都不错,但在 iPad 上会拉伸和倾斜节点以及视图周围的边界框。
  2. Changing the scaleMode among all four types.在所有四种类型之间更改 scaleMode。 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.我想保持良好的实践,并觉得 .AspectFill(默认)是我应该让我的应用程序使用的那个,但对建议持开放态度。 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.现在我对此很陌生,完全不了解约束,但是我什至从 RayWenderlich 看到的教程都没有讨论节点上的约束,所以我没有深入研究。
  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.此外,当我使用这种方法为 iPad 构建时,视图似乎开始时是纵向的,节点看起来不错,但随后我必须手动将其切换为横向,并且精灵和视图边界再次变得混乱。 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) }
  5. Countless vid tutorials on RW and everywhere else on internet.无数关于 RW 和互联网上其他地方的视频教程。

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.我知道这个话题很奇怪,因为很多人都会问这个问题,但每个人的情况似乎都不同,以至于一种解决方案并不适合所有人。

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).我最初尝试用 2 个游戏自己进行缩放,但这太疯狂了(场景大小 = 视图大小或场景缩放模式 = .ResizeFill)。 You have to adjust all values eg font size, sprite sizes, impulses etc for all devices and it will never be consistent.您必须调整所有设备的所有值,例如字体大小、精灵大小、脉冲等,而且永远不会保持一致。

So you have 2 options basically所以你基本上有2个选择

1) Set scene size to 1024X768 (landscape) or 768x1024 (portrait). 1)将场景尺寸设置为 1024X768(横向)或 768x1024(纵向)。 This was the default setting in Xcode 7.这是 Xcode 7 中的默认设置。

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:在 iPad 上显示更多/在 iPhone 上裁剪的游戏示例:

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

2) Apple changed the default scene size in xCode 8 to iPhone 6/7 (750 1334-Portait, 1337 750-Landscape). 2) Apple 将xCode 8 中的默认场景大小更改为 iPhone 6/7 (750 1334-Portait, 1337 750-Landscape)。 This setting will crop your game on iPads.此设置将在 iPad 上裁剪您的游戏。

Examples of games that show less on iPads:在 iPad 上显示较少的游戏示例:

Lumino City, Robot Unicorn Attack鲁米诺城,机器人独角兽来袭

Chosing between the 2 options is up to you and depends what game you are making.在 2 个选项之间进行选择取决于您,并取决于您正在制作的游戏。 I usually prefer to use option 1 and show more background on iPads.我通常更喜欢使用选项 1 并在 iPad 上显示更多背景。

Regardless of scene size scale mode is usually best left at the default setting of .aspectFill.无论场景大小,缩放模式通常最好保留 .aspectFill 的默认设置。

To adjust specific things such as labels etc you can do it this way要调整标签等特定内容,您可以这样做

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

You can try the scene scaling yourself, create a new SpriteKit sample game project.您可以尝试自己缩放场景,创建一个新的 SpriteKit 示例游戏项目。 Run on all iPhones and you will notice the HelloWorld label looks perfect on all devices.在所有 iPhone 上运行,您会注意到 HelloWorld 标签在所有设备上看起来都很完美。

Now change the default settings to scene size = frame or use .ResizeFill, the HelloWorld label is not scaled properly anymore on all devices.现在将默认设置更改为场景大小 = 帧或使用 .ResizeFill,HelloWorld 标签不再在所有设备上正确缩放。

As a side note, the line作为旁注,该行

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

references the GameScene.sks file.引用 GameScene.sks 文件。 You said you do everything programatically, therefore you can probably delete the GameScene.sks file and change the line to您说您以编程方式完成所有操作,因此您可以删除 GameScene.sks 文件并将该行更改为

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

Update:更新:

I am now using a slightly different variant as I had problems adapting my game to iPhoneX.我现在使用稍微不同的变体,因为我在将游戏适配到 iPhoneX 时遇到了问题。 I set scene size to 1334x750 and use aspect fit as scale mode.我将场景大小设置为 1334x750,并使用宽高比作为缩放模式。 I than run some code to remove the black bars if needed eg on iPads or iPhone X. It's based on this great article (link no longer works).如果需要,我会运行一些代码来删除黑条,例如在 iPad 或 iPhone X 上。它基于这篇很棒的文章(链接不再有效)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM