【问题标题】:Border around screen and not frame?屏幕周围的边框而不是框架?
【发布时间】:2016-01-20 21:29:57
【问题描述】:

我目前正在使用 Swift/SpriteKit 制作游戏,但遇到了问题。每当我为我的游戏制作边框时,边框总是围绕 iPhone/iPad FRAME 而不是屏幕。因此,当我的球弹起时,它会离开屏幕并从“iPhone FRAME”而不是“场景/框架”弹回。

有人可以帮我制作,让我的边界在 SCENE 周围而不是 FRAME 周围吗?

这是我的代码:

let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

borderBody.friction = 0

self.physicsBody = borderBody

【问题讨论】:

  • 你介意用物理显示拍张照片来证明你的意思吗?您正在使用 self.frame ,它应该返回场景的帧,所以在这种情况下,绕过场景和绕过框架将是一回事......
  • 虽然框架比场景大。球不会从屏幕边缘反弹,它会向后更远
  • 听起来问题可能出在您初始化场景时。可能是尺寸不对。
  • 简而言之,尝试使用 skView.bounds.size 初始化场景,并记住有时在 viewDidLoad 方法中可能不知道视图的最终大小。您可以改用 viewWillLayoutSubviews。搜索有关此的内容。此外,如果启动图像设置不正确,有时视图可能会出现错误大小,但我怀疑这与您当前的问题有一些关系。
  • 每当我尝试初始化场景时,都没有名为 size 的初始化程序

标签: swift sprite-kit


【解决方案1】:

我的意思是(在 cmets 中)您应该设置场景的大小以匹配视图的大小,如下所示:在 GameViewController.swift 中

override func viewDidLoad() {
        super.viewDidLoad()

        if let scene = GameScene(fileNamed:"GameScene") {
            // Configure the view.
            let skView = self.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 = .AspectFill

            scene.size = skView.bounds.size

            skView.presentScene(scene)
        }
    }

注意scene.size = skView.bounds.size,它将场景的大小设置为视图的大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多