【问题标题】:Adding a background image to a SpriteKit project向 SpriteKit 项目添加背景图像
【发布时间】:2016-11-11 10:52:53
【问题描述】:

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)

我做错了什么?

【问题讨论】:

  • 在您发布的代码中,“self.size”中的 self 是什么。是SKScene吗?
  • @MrFlynn 是的,这是 SKScene

标签: ios swift sprite-kit resolution


【解决方案1】:

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

【讨论】:

  • 不不不,您永远不应该将场景大小设置为您的 SKView 大小,resizefill 会为您完成此操作
  • 是的,你有理由,它也可以混淆OP,我删除它,谢谢Knight0fDragon。
  • NP,我可以看到为视图设置场景大小的唯一场景是在 OSX 的情况下,我们有一个可以调整大小的窗口,出于某种原因,您希望保留场景方面您启动应用的比率
【解决方案2】:

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

【讨论】:

  • 但是这个改变影响了 iPhone 4s 的结果。 OP也可以选择iPhone项目,不是通用的但是还要考虑4s设备,还是可以的。
  • 从这个帖子看,OP不关心iphone 4,所以我猜是丢掉图片的顶部和底部是可以接受的
  • 你在说什么?
  • 您的回答是:“进入您的 .sks 文件,并将场景大小更改为 9x16 的比例......”。我不喜欢使用 SKS 文件进行更改,我说我更喜欢代码,似乎像 spriteBuilder -Cocos2D 一样工作,你知道吗?
  • 那么你真的应该采用将呈现与功能分离的模型,它允许你重新设计外观而无需更改代码。但是如果你想做代码,那么当你创建你的 SKScene 时,你只需将大小设置为 9:16 大小。 let scene = SKScene(size:CGSizeMake(320,568)) 或者在 OP 的情况下:CGSizeMake(375,667) (记住我们正在做 Retina 风格的编码,所以我们做的是点而不是像素)
猜你喜欢
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 2023-02-21
  • 2011-04-24
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
相关资源
最近更新 更多