In Sprite Kit, I'm using an SKCameraNode to zoom in and out of an SKScene. Any SKLightNodes in the scene are not zoomed at all. They are not children of the Camera so should not be invariant. Can't find anything on this - Search "SKLightNode SKCameraNode" on SO yields 0 results.
Using Xcode 8.3.3 and starting from a basic Game/Swift project, I have replaced sceneDidLoad() in GameScene.swift with:
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)
}
As you can see, the light stays the same during the zooms.
In an attempt to fix this problem, I've tried the following custom action to modulate the falloff property of the light. It's sort of ok but it's not a faithful zoom.
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)
Surely the camera should zoom on the light? Am I missing something?
EDIT: following suggestions below here is some code that scales the 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)
You will also need to halve the Camera zoom. The only problem with this is that everything is scaled (even nodes added as children to the CameraNode like scores/buttons).