简体   繁体   English

SpriteKit中设备的基本定位

[英]Basic Positioning for Devices in SpriteKit

I'm extremely new to SpriteKit, and have been following some great tutorials by someone online. 我对SpriteKit极为陌生,并且一直在关注在线某人的一些出色教程。 They all do one thing which I think might be a red flag, but wanted to ask the community about. 他们都做一件事,我认为这可能是一个危险信号,但想向社区询问。

Prerequisite: all his demos are done using iPad (it's set in the app settings to only target iPad, so no intention of iPhone). 先决条件:他的所有演示都使用iPad(在应用程序设置中设置为仅针对iPad,因此无意使用iPhone)。

So what i'm seeing is the developer creates labels and graphics by using hard-coded points. 因此,我所看到的是开发人员通过使用硬编码的点来创建标签和图形。

For example to make the text "game over" appear: 例如,使文本“ game over”出现:

            let gameOver = SKSpriteNode(imageNamed: "gameOver")
            gameOver.position = CGPoint(x: 512, y: 384)
            gameOver.zPosition = 1
            addChild(gameOver)

So the thing about this code is it's hard-coded to be at a specific point. 因此,有关此代码的事情是将其硬编码为特定点。 So this means that in theory, it's going to be slightly wrong on the iPad mini vs. the iPad pro, correct (since he intended this to be on the iPad air)? 因此,这意味着从理论上讲,在iPad mini和iPad Pro上会有些错误,是正确的(因为他打算在iPad上播放)? Or do the devices translate where that should be every time? 还是设备每次都翻译该在哪里?

I feel like you'd want to do some fancy magic like calculate the width/height of the screen and divide by some number to calculate the center, rather than specify points. 我觉得您想做一些花哨的魔术,例如计算屏幕的宽度/高度并除以某个数字以计算中心,而不是指定点。

In fact, all of his code more or less uses hard-coded points for example look at this to render sprites: 实际上,他的所有代码或多或少都使用了硬编码的点,例如看一下它来渲染精灵:

        for i in 0 ..< 5 { drawSpriteAt(CGPoint(x: 100 + (i * 170), y: 410)) }
        for i in 0 ..< 4 { drawSpriteAt(CGPoint(x: 180 + (i * 170), y: 320)) }
        for i in 0 ..< 5 { drawSpriteAt(CGPoint(x: 100 + (i * 170), y: 230)) }

Is this not a major problem if I were targeting iPad mini and iPad pro as well? 如果我也同时瞄准iPad mini和iPad Pro,这不是主要问题吗?

For some reason, however, I ran this on the simulator in iPad pro and it actually looked pretty good. 但是由于某种原因,我在iPad Pro的模拟器上运行了它,实际上看起来还不错。

So my question, ultimately then, is: 因此,我的问题最终是:

Is this just fine to do it this way? 这样行就行吗? Do the different iPads somehow magically work out what the point (512, 384) is such that that point is different on an iPad mini vs. iPad pro? 不同的iPad是否以某种方式神奇地算出了一点(512、384),以至于iPad mini与iPad pro的这一点不同?

Thanks and apologies if this is a basic question, I just wanted to see that if I were only writing an app to target iPhones, or iPads (i'm doing both actually) that I would make two separate apps with hard-coded coordinates specific for one device. 谢谢和道歉,如果这是一个基本问题,我只是想看看,如果我只编写针对iPhone或iPad的应用程序(我实际上同时在做),我将制作两个单独的应用程序,这些应用程序具有特定的硬编码坐标一台设备。

For example i'd say "on all iPads, regardless, use point (512,384)" and on an iPhone i'd say "on all iPhones of all sizes, use point (300, 150) or something like that. 例如,我会说“在所有iPad上,无论使用点(512,384)”,在iPhone上我都会说“在所有尺寸的所有iPhone上,使用点(300、150)或类似的东西。

Thanks so much for your help! 非常感谢你的帮助!

Sprite Kit is designed to have a scaling system so you don't have to worry as much about coordinates. Sprite Kit设计为具有缩放系统,因此您不必担心坐标。 The coordinates don't represent pixel or point coordinates on the screen - but are coordinates in the game scene. 坐标不代表屏幕上的像素或点坐标,而是游戏场景中的坐标。 Basically if you setup a scaling mode for you game, your coordinates will be taken care of for multiple screen sizes. 基本上,如果您为游戏设置了缩放模式,则将在多个屏幕尺寸下处理您的坐标。 (You would be correct in your assumption if you were just talking about iOS button/view coordinates.) (如果您只是在谈论iOS按钮/视图坐标,则您的假设是正确的。)

See the documentation here: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html 请参阅此处的文档: https : //developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html

After a scene is rendered, its contents are copied into the presenting view. 渲染场景后,其内容将复制到呈现视图中。 If the view and the scene are the same size, then the content can be directly copied into the view. 如果视图和场景的大小相同,则可以将内容直接复制到视图中。 If the two differ, then the scene is scaled to fit in the view. 如果两者不同,则将场景缩放以适合视图。 The scaleMode property determines how the content is scaled. scaleMode属性确定内容的缩放方式。

When you design your game, you should decide on a strategy for handling the scene's size and scaleMode properties. 在设计游戏时,应确定一种用于处理场景的size和scaleMode属性的策略。 Here are the most common strategies: 以下是最常见的策略:

  • Instantiate the scene with a constant size and never change it. 以恒定的大小实例化场景,并且永远不要更改它。
  • Pick a scaling mode that lets the view scale the scene's content. 选择一种缩放模式,使视图可以缩放场景的内容。 This gives the scene a predictable coordinate system and frame. 这为场景提供了可预测的坐标系和框架。 You can then base your art assets and gameplay logic on this coordinate system. 然后,您可以在此坐标系上建立艺术资产和游戏逻辑。
  • Adjust the size of the scene in your game. 调整游戏中场景的大小。 Where necessary, adjust your game logic and art assets to match the scene's size. 必要时,调整游戏逻辑和美术资产以匹配场景的大小。
  • Set the scaleMode property to SKSceneScaleModeResizeFill. 将scaleMode属性设置为SKSceneScaleModeResizeFill。 SpriteKit automatically resizes the scene so that it always matches the view's size. SpriteKit会自动调整场景的大小,使其始终与视图的大小匹配。 Where necessary, adjust your game logic and art assets to match the scene's size. 必要时,调整游戏逻辑和美术资产以匹配场景的大小。

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

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