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

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

问题描述

我想将 风景应用设为通用,以便精灵节点按比例缩放到运行应用的任何视图大小。我想要一个完全程序化的解决方案,因为我不喜欢IB。



我的游戏非常简单,我不需要任何类型的滚动或缩放,因此整个游戏将始终存在并占据整个视图。 / p>

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

 如果让scene = GameScene( fileNamed:GameScene)

是构造函数方法,它将size作为参数,但Xcode没有'这样。






我尝试过的事情


  1. 使用self.view.bounds.width / height的分数。这通常会使所有iPhone看起来都很好,但是在iPad上拉伸和扭曲节点以及视图周围的边界框。

  2. 更改所有四种类型中的scaleMode。我想保持良好的练习并且感觉像.AspectFill(默认)是我应该让我的应用程序工作,但开放的建议。注意;我不希望任何设备上出现黑边,只是按比例显示/缩放整个视图。

  3. 应用程序化约束。现在我对这个很新,并且不完全理解约束,但是我甚至没有看过RayWenderlich的教程,谈论节点上的约束,所以我没有深入研究这个。

  4. 使用这样的方法在视图之间转换点。这对于节点的点定位实际上非常有效,如果可能的话,我希望这种方法可以解决,但是我仍然遇到节点大小的问题。此外,当我使用这种方法为iPad构建时,视图似乎从纵向开始,节点看起来很好但是我必须手动将其切换到横向,精灵和视图边界再次搞砸了。这是方法:

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


  5. RW及其他任何地方的无数视频教程。


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

解决方案

我最初尝试使用2个游戏进行缩放,这只是疯狂(场景大小=视图大小或场景缩放模式= .ResizeFill)。您必须调整所有设备的所有值,例如字体大小,精灵大小,脉冲等,它们永远不会一致。



所以你基本上有2个选项



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



您通常只在顶部/底部(横向)或左/右(纵向)上显示/显示一些额外的背景iPhone上的iPhone会被裁剪掉。



在iPad上显示更多内容的游戏示例:



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



2) Apple将xCode 8中的默认场景大小更改为iPhone 6/7(750 * 1334-Portait,1337 * 750-Landscape)。此设置将在iPad上裁剪您的游戏。



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



Lumino City ,机器人独角兽攻击



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



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



要调整标签等特定内容,您可以这样做

 如果UIDevice.current.userInterfaceIdiom == .pad {
...
}

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



现在将默认设置更改为scene size = frame或使用.ResizeFill,HelloWorld标签在所有设备上都不再正确缩放。



作为旁注,行

 如果让scene = GameScene(fileNamed:GameScene) 

引用GameScene.sks文件。你说你以编程方式做所有事情,因此你可以删除GameScene.sks文件并将行更改为

 让skView = view如! SKView! 
让场景= GameScene(尺寸:CGSize(宽度:1024,身高:768))// 768 x 1024如果肖像

更新:



我现在使用的变体略有不同,因为我在将游戏改编为iPhoneX时遇到了问题。我将场景大小设置为1334x750并使用宽高比适合作为缩放模式。我运行一些代码来删除黑条,如果需要,例如iPad或iPhone X,因为我更喜欢显示更多的背景。它基于这篇伟大的文章。



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


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")

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


Things I've tried

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

  5. 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.

解决方案

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) Set scene size to 1024X768 (landscape) or 768x1024 (portrait). This was the default setting in 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.

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

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). This setting will crop your game on iPads.

Examples of games that show less on iPads:

Lumino City, Robot Unicorn Attack

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.

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 {
   ...
 }

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.

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

As a side note, the line

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

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

Update:

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 need eg iPad or iPhone X as I prefer to show more background. It’s based on this great article.

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

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

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