SKLightNode不使用SKCameraNode进行缩放

时间:2017-06-11 23:50:32

标签: ios swift sprite-kit

在Sprite Kit中,我使用SKCameraNode来放大和缩小SKScene。场景中的任何SKLightNodes都不会被缩放。他们不是相机的孩子,所以不应该是不变的。找不到任何相关内容 - 在SO上搜索“SKLightNode SKCameraNode”会得到0结果。

使用Xcode 8.3.3并从基本的Game / Swift项目开始,我已将GameScene.swift中的sceneDidLoad()替换为:

override func sceneDidLoad() {

    let cameraNode = SKCameraNode()
    cameraNode.position = CGPoint(x:0.0, y:0.0)
    self.addChild(cameraNode)
    self.camera = cameraNode

    let bg = SKSpriteNode(color:.red, size:self.size)
    bg.lightingBitMask = 0b0001
    self.addChild(bg)

    let lightNode = SKLightNode()
    lightNode.position = CGPoint(x:0.0, y:0.0)
    lightNode.categoryBitMask = 0b0001
    lightNode.lightColor = .white
    lightNode.falloff = 1.0
    self.addChild(lightNode)

    let zoomDuration:TimeInterval = 10.0
    let zoomAction = SKAction.sequence([
        SKAction.scale(to:0.25, duration:zoomDuration),
        SKAction.scale(to:1.0, duration:zoomDuration)
        ])
    self.camera?.run(zoomAction)

}

如您所见,在变焦期间灯光保持不变。

为了解决这个问题,我尝试了以下自定义操作来调制灯光的衰减属性。这有点好,但它不是一个忠实的缩放。

    let lightAction1 = SKAction.customAction(withDuration: zoomDuration) {
        (node, time) -> Void in
        let lightNode = node as! SKLightNode
        let ratio:CGFloat = time / CGFloat(zoomDuration)
        let startFalloff:CGFloat = 1.0
        let endFalloff:CGFloat = 0.25
        let falloff:CGFloat = startFalloff*(1.0-ratio) + endFalloff*ratio
        lightNode.falloff = falloff
    }
    let lightAction2 = SKAction.customAction(withDuration: zoomDuration) {
        (node, time) -> Void in
        let lightNode = node as! SKLightNode
        let ratio:CGFloat = time / CGFloat(zoomDuration)
        let startFalloff:CGFloat = 0.25
        let endFalloff:CGFloat = 1.0
        let falloff:CGFloat = startFalloff*(1.0-ratio) + endFalloff*ratio
        lightNode.falloff = falloff
    }
    let lightSequence = SKAction.sequence([lightAction1, lightAction2])
    lightNode.run(lightSequence)

相机应该放大灯吗?我错过了什么吗?

编辑:以下建议是一些扩展SKView的代码:

    let originalWidth:CGFloat = UIScreen.main.bounds.width
    let originalHeight:CGFloat = UIScreen.main.bounds.height

    let lightAction1 = SKAction.customAction(withDuration: zoomDuration) {
        (node, time) -> Void in
        let ratio:CGFloat = time / CGFloat(zoomDuration)
        let startFalloff:CGFloat = 1.0
        let endFalloff:CGFloat = 1.5
        let falloff:CGFloat = startFalloff*(1.0-ratio) + endFalloff*ratio
        self.view?.frame = CGRect(x: (originalWidth-originalWidth*falloff)/2.0, y: (originalHeight-originalHeight*falloff)/2.0, width: originalWidth*falloff, height: originalHeight*falloff)
    }
    let lightAction2 = SKAction.customAction(withDuration: zoomDuration) {
        (node, time) -> Void in
        let ratio:CGFloat = time / CGFloat(zoomDuration)
        let startFalloff:CGFloat = 1.5
        let endFalloff:CGFloat = 1.0
        let falloff:CGFloat = startFalloff*(1.0-ratio) + endFalloff*ratio
        self.view?.frame = CGRect(x: (originalWidth-originalWidth*falloff)/2.0, y: (originalHeight-originalHeight*falloff)/2.0, width: originalWidth*falloff, height: originalHeight*falloff)
    }
    let lightSequence = SKAction.sequence([lightAction1, lightAction2])
    lightNode.run(lightSequence)

您还需要将相机缩小一半。唯一的问题是所有内容都被缩放(甚至将节点作为子节点添加到CameraNode中,如分数/按钮)。

1 个答案:

答案 0 :(得分:2)

我想感谢您提供我们可以测试的示例。通过玩它,我可以肯定地看到光线在增长和缩小,但它并不是很多,(如果你将相机移动到位置(0,0)则更明显)也许是苹果末端的数学问题,或者绘图顺序优先问题。

我甚至尝试将SKLightNode添加到单独的SKNode中,并缩放节点,但结果相同。

我还尝试将场景调整为更大的尺寸,但结果仍然相同。

然后我决定玩视图大小,低,看到我调整后能够得到不同的结果。这意味着SKLightNode不受SKView尺寸的影响,而不是SKScene尺寸(真的是Apple?)。无论是那还是它都脱离了SKView提供的环境。无论哪种方式,看起来你没有使用SKLightNode进行缩放。 (顺便说一句,阴影仍然会变焦,所以也许你只需要在更好的照明效果下工作?)