解释
出于某种原因,我的游戏中设置为横向的宽度和高度值正在交换(如下所示)。这样,本应位于屏幕中央的 Sprite 就完全偏离了正确的位置。
代码
可以下载here .
游戏场景
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
print("width:", self.frame.width)
print("height:", self.frame.height)
//Sprite
let sprite = SKSpriteNode (imageNamed: "sprite")
sprite.anchorPoint = CGPointZero
sprite.position = CGPoint(x: self.frame.width / 2 - sprite.frame.width / 2, y: self.frame.height / 2 - sprite.frame.height / 2)
addChild(sprite)
}
}
游戏 View Controller
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Set view size.
let scene = GameScene(size: view.bounds.size)
// Configure the view.
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .Landscape
} else {
return .Landscape
}
}
}
提前致谢,
路易斯。
最佳答案
如果您转到 xCode 项目中的 targets-General(您在其中设置版本号、 bundle ID 等),您是否取消了肖像?您可能还必须检查您的 info.plist 以查看是否为这两种设备删除了所有肖像条目。
不幸的是,很多教程都教过你,你的做法是不好的做法,我不会继续这样。
如果您使用“scaleMode ResizeFill”或将场景大小设置为“view.bounds”,您的游戏将永远不会在所有设备上保持一致。此外,您的所有值( Sprite 大小、字体大小、物理值等)在与您正在测试的设备不同的设备上也不会相同。
基本上,您将不得不针对 5-6 台设备上的所有这些及其疯狂程度进行调整,尤其是使用 xCode 模拟器时。相信我,我之前去过那里玩过 2 场比赛,差点要了我的命。这是一场“是的,这看起来不错”的游戏。
不要经历这个,所以你应该做的是
1) 将场景大小更改为 xCode 使用的默认场景大小
GameScene(size: CGSize(width: 1024, height: 768)) // swap if portrait
注意:在底部检查更新
If you dont do this, and leave it at view.bounds.size, point 2 will not work.
2) 将场景缩放模式更改为 .AspectFill(也是默认的 xCode 设置)。
这样一来,您的所有内容在所有 iPhone 上看起来都很棒。您的值将正确缩放,并为您节省大量工作。 在 iPad 上,您将在顶部和底部(横向)或左右(纵向)有一些额外的空间,您通常只是用更多的背景覆盖这些空间并将其作为不可玩区域
你的场景现在基本上是这样的。
红色区域是您的整个场景大小 (iPad),但如果您在 iPhone 上运行,您只会看到绿色部分。在人像模式下,红色区域位于左侧和右侧。
这就是为什么通常从中心进行 y 定位的原因,因为如果您使用 frame.minY 或 frame.maxY,您将处于红色区域并且不会在 iPhone 上看到 Sprite 。您刚刚用更多背景覆盖的红色区域(您的背景全屏图像应该是 iPad 大小)。
这也会使您的游戏更加平衡,因为如果您只是拉伸(stretch)游戏,那么在 iPad 上会更容易,因为您有更多空间。
因此,请在绿色区域内设计您的游戏。与 iPad 相比,您可能唯一需要做的就是向上移动一些 UI,例如暂停按钮或分数标签,当您想要在顶部边缘显示它们时。我是这样做的。
// Some button
...
someButton.position = CGPoint(x: frame.midX, y: frame.midY + 200)
addChild(someButton)
if UIDevice.current.userInterfaceIdiom == .pad {
// set for iPad
}
// Some label
someLabel.positon = CGPoint(x: frame.midX - 100, someButton.position.y) // because its y position is the same as someButton I only have to adjust someButton.position.y on iPads.
addChild(someLabel)
我不会继续采用您的方法,即使这意味着需要重做很多工作。
更新:
苹果似乎在 xCode 8 中将默认场景大小从 1024x768 (768x1024) 更改为 1334x750 (750x1334)。我只对这些设置进行了一些尝试,对于一款通用游戏来说,它们似乎令人困惑。
所以你的场景现在看起来像这样,iPad 上 xAxis 上的东西在屏幕外。
这毫无意义,因为 iPad 显然并不比 iPhone 更宽屏。 我实际上打开了一个错误报告,看看苹果对此有何评论,所以现在我可能会将其保持在 1024x768。
希望对你有帮助
关于ios - 交换宽度和高度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39430799/