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).
I would like to thank you for providing an example we can test. By playing around with it, I can definitely see the light growing and shrinking but it is not by much, (it is more noticeable if you move the camera off position (0,0) Perhaps theres a math problem on apples end, or a drawing order priority problem.
I have even attempted to add the SKLightNode to a separate SKNode, and scale the node, but that left the same result.
I also attempted to resize the scene to a larger size, and still the same result.
I then decided to play with the view size, low and behold I was able to get different results when I adjusted this. This means that SKLightNode goes off of the SKView size, and not the SKScene size (Really apple?). Either that or it goes off of the context that the SKView provides. Either way, looks like you are not zooming with SKLightNode. (Btw, the shadow still zooms, so perhaps you just need to work in a better lighting affect?)