[英]Adding a background image to a SpriteKit project
iPhone 6和6s应该具有750 x 1334的分辨率[1] ,自iPhone 5以来每部iPhone的屏幕比例为16:9 [2] 。 因此,为了使应用程序的背景图像完美匹配,它必须具有16:9的比例。 我正在使用SpriteKit进行一个项目,我希望游戏有一个从边缘到边缘覆盖背面的壁纸。 但是,当我在模拟器上运行应用程序时,背景图像总是在左右两侧被裁剪。 我甚至尝试过各种各样的比率和分辨率。 该项目背景的代码是:
let background = SKSpriteNode(imageNamed: "backtImage")
background.size = self.size
background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
background.zPosition = 0
self.addChild(background)
我究竟做错了什么?
这可能是因为您的SKView
与您的SKScene
尺寸不匹配。
消息来源:
@available(iOS 7.0, *)
public enum SKSceneScaleMode : Int {
case Fill /* Scale the SKScene to fill the entire SKView. */
case AspectFill /* Scale the SKScene to fill the SKView while preserving the scene's aspect ratio. Some cropping may occur if the view has a different aspect ratio. */
case AspectFit /* Scale the SKScene to fit within the SKView while preserving the scene's aspect ratio. Some letterboxing may occur if the view has a different aspect ratio. */
case ResizeFill /* Modify the SKScene's actual size to exactly match the SKView. */
}
您可以更改以下代码:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
...
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .ResizeFill // <- this is an example
skView.presentScene(scene)
}
}
如果您的图像在左侧和右侧被裁剪(在纵向中)那么我们正在处理.AspectFill缩放模式,并且您的场景方面比屏幕分辨率短。 这可能是因为模板构建中提供的默认场景大小是正方形。 进入你的.sks文件,并将场景大小更改为9x16比例,你的问题应该解决
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.