将背景图像添加到SpriteKit项目

时间:2016-07-08 19:19:41

标签: ios swift sprite-kit resolution

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)

我做错了什么?

2 个答案:

答案 0 :(得分:0)

这可能是因为您的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)
        }
    }

答案 1 :(得分:0)

如果您的图像在左侧和右侧被裁剪(在肖像中)那么我们正在处理.AspectFill缩放模式,并且您的场景方面比屏幕分辨率短。这可能是因为模板构建中提供的默认场景大小是正方形。进入.sks文件,将场景大小更改为9x16比例,并解决您的问题

相关问题