不要根据 iPhone 型号调整场景大小,让 Sprite-kit 做这种工作:
scene.scaleMode = SKSceneScaleMode.ResizeFill
场景未缩放以匹配视图。取而代之的是场景
自动调整大小,使其尺寸始终与
视图。
关于大小,当一个场景第一次初始化时,它的size属性是由指定的初始化器来配置的。场景大小以点为单位指定场景可见部分的大小。这仅用于指定场景的可见部分。
更新以帮助您定位:
如果你想设置你的位置而不是使用 scaleMode 你可以
将您的 scene.scaleMode 设置为 .AspectFill 以使其适用于所有场景,并且场景大小必须为 2048x1536 或 1536x2048。这将使它也适用于 iPad。
class StartScene: SKScene {
let playableArea: CGRect!
}
override init(size: CGSize) {
//1. Get the aspect ratio of the device
let deviceWidth = UIScreen.mainScreen().bounds.width
let deviceHeight = UIScreen.mainScreen().bounds.height
let maxAspectRatio: CGFloat = deviceWidth / deviceHeight
//2. For landscape orientation, use this
let playableHeight = size.width / maxAspectRatio
let playableMargin = (size.height - playableHeight) / 2.0
playableArea = CGRect(x: 0, y: playableMargin, width: size.width, height: playableHeight)
//3. For portrait orientation, use this
let playableWidth = size.height / maxAspectRatio
let playableMargin = (size.width - playableWidth) / 2.0
playableArea = CGRect(x: playableMargin, y: 0, width: playableWidth, height: size.height)
super.init(size: size)
}
因此您可以使用以下方式定位您的对象:
ball.position = CGPoint(x: CGRectGetMidX(playableArea), y: CGRectGetMaxY(playableArea) - (ball.size.height * 0.90))
此代码适用于 iPhone 4S、5、5S、6、6 Plus、6S、6S Plus 和 iPad。
如果您想查看边框(是否用于调试):
func drawWorkArea() {
let shape = SKShapeNode()
let path = CGPathCreateMutable()
CGPathAddRect(path, nil, workArea)
shape.path = path
shape.strokeColor = SKColor.redColor()
shape.lineWidth = 8
addChild(shape)
}