简体   繁体   English

在场景上定位节点以适合所有屏幕尺寸SpriteKit

[英]Positioning nodes on a scene to work for all screen sizes SpriteKit

I have tried everything and nothing seems to work for positioning nodes for a universal SpriteKit game. 我已经尝试了所有方法,但似乎对于定位通用SpriteKit游戏的节点没有任何作用。 I currently use .AspectFill as my scaling ratio and I have tried making unnecessarily complex algorithms to fit nodes (eg buttons, a title image, etc) for my main menu in my game into bounds or sub sections of the scene. 我目前使用.AspectFill作为缩放比例,并且尝试制作不必要的复杂算法以使游戏主菜单的节点(例如按钮,标题图像等)适合场景的边界或子部分。 I have even tried positioning things relative to the visible parts of the scene. 我什至尝试相对于场景的可见部分定位事物。 eg 例如

 sprite.position = CGPoint(scene.size.width/2, scene.size.height/2 + scene.size.height * 0.4) 

and yes this works for gameplay elements such as a player I have figured that this method is horribly unpredictable for positioning HUD and menu screen elements as buttons then tend to overlap because of dynamic sizing of sprites and/or unknown scaling of scene depending on user's device (If I get it perfectly positioned on iPhone it goes off of the screen on iPad and vice versa) eg 是的,这适用于诸如玩家之类的游戏元素,我认为这种方法对于放置HUD和菜单屏幕元素非常不可预测,因为按钮的大小会动态重叠和/或未知的场景缩放(取决于用户的设备),因此按钮可能会重叠(如果我将其完美地放置在iPhone上,它将在iPad上离开屏幕,反之亦然)例如

sprite.size = CGSize(screen.size.width * 0.6, screen.size.width * 0.6)

I then went onto making an extension of SKSpriteNode so that it can resize itself depending on a given bounds (CGRect) and then I realised just how complex, messy and time consuming positioning one node on my scene was. 然后,我继续进行SKSpriteNode的扩展,以便可以根据给定范围(CGRect)调整自身大小,然后我意识到在场景中定位一个节点是多么复杂,混乱且耗时。 I must have the complete wrong idea of positioning nodes in my scene or I am doing it wrong. 我必须完全错误地将节点定位在场景中,否则我做错了。 Now you understand the context here are my questions: 现在您了解了上下文,这是我的问题:

  • Should I hardcode the positions (I have heard that this isn't a great method but since SpriteKit does the scaling for you shouldn't it work) ? 我应该对位置进行硬编码(我听说这不是一个很好的方法,但是由于SpriteKit为您缩放了它不起作用)?
  • Or should I create a dynamic position depending on the aspect ratio of each device eg. 或者我应该根据每个设备的长宽比创建动态位​​置,例如。

if scene.size.width/scene.size.height >= 16/9 { //Create position here for this device } else if scene.size.width/scene.size.height >.... { etc etc

This method also seems unnecessary but I could be wrong 这种方法似乎也不必要,但我可能是错的

OR 要么

  • Is the solution some other method that I am not aware of 解决方案是我不知道的其他方法吗

I cannot be the only person with this problem and from doing research for hours and hours reading every single stackoverflow answer and webpage Google returns to me that might have my answer I still have not found a solution that works for my problem. 我不可能是唯一遇到此问题的人,并且从研究中花了好几个小时阅读每个Google Stackoverflow答案和Google可能返回我答案的网页,但我仍然找不到适合我问题的解决方案。 This question has been asked plenty of times but obviously the answers do not match to my learning style and it needs to be explained in a different way so that I finally understand it and with the amount of apps on the app store surely someone has an easy solution to such a stressful problem that is causing me to feel unmotivated to make my app and that shouldn't be the case. 这个问题已经被问了很多次了,但是显然答案与我的学习风格不符,需要用不同的方式来解释它,以便我最终理解它,并且应用商店中的应用数量一定可以使人轻松解决这种压力大的问题的方法,这种问题使我感到无心开发我的应用程序,事实并非如此。

Since you're using AspectFill as your scaleMode, the scene automatically scales it. 由于您将AspectFill用作scaleMode,因此场景会自动对其进行缩放。 With this, you would then make the screen size 768x1024 (portrait) or 1024x768 (landscape). 这样,您便可以将屏幕尺寸设置为768x1024(纵向)或1024x768(横向)。 To make this compatible with all devices simply make the background 1024x1024 so it looks fine on iPad and you can adjust HUD elements for iPad only by doing something like this: 要使其与所有设备兼容,只需将背景设置为1024x1024,即可在iPad上看起来不错,并且只能通过执行以下操作来为iPad调整HUD元素:

if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
    yourButton.position.y += 100
    yourLabel.position.y += 100
    etc
 }

You don't need any complex algorithms and the scene looks great on all screens. 您不需要任何复杂的算法,并且所有屏幕上的场景看起来都很棒。 I also suggest you change the anchorPoint of the screen to the middle so you position all the nodes from the middle rather than from the bottom left corner as this makes it much easier to centre things for all devices 我还建议您将屏幕的anchorPoint更改为中间位置,以便将所有节点从中间位置而不是从左下角放置,因为这样可以更轻松地将所有设备的内容居中

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

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