2
votes

I'm trying to create a spotlight effect for an iOS game with Sprite Kit. The goal is to have a background which is revealed by several moving spotlights bouncing on the edge of the screen.

I have successfully found a solution for one moving spotlight either using a SKCropNode or more simply a SKSpriteNode.

For the SKSpriteNode solution, I just added on top of my scene (i.e. it is the last child added) an SKSpriteNode which is an image larger (1.5 length and 1.5 width) than the scene. The image is a black rectangle with a transparent circle in the middle (using the alpha channel). I added a physic body equal to the circle size. I gave it an impulse and SpriteKit does the rest. I have then a nice spotlight bouncing around and revealing everything (i.e. any sprite added to the scene) which is behind (or below) the transparent circle.

For the SKScropNode solution, I created a SKCropNode to which I added the background sprites. Then I defined the mask of the SKSCropNode with an image made of just a transparent circle. I also added a physic body equal to the circle size, gave it an impulse and it worked. Almost in fact, because I couldn't find a way to have the non transparent part of the mask being something else than the background color. I want it black like I did with the SKSpriteNode solution, even if the background color of the scene is not black.

I also tried with a SKLightNode but the result is not as good as the above solutions as the light is diffusing to much and does not allow to have precise circle of light. I want fully transparent inside and completely black outside, no gradient from transparent to black.

My main issue is that I need to have 3 bouncing spotlights and not only one.

With the SKSpriteNode solution, if I add 3 SKSpriteNode, the black part of the SKSpriteNode which is on the top is covering the white circles of the 2 other SKSpriteNode below.

With the SKCropNode solution, you can have only one mask per SKScropNode. I have tried to cascade SKScropNode but it does not work.

Is there any other option to achieve this effect (in swift) ?

Thanks for any suggestion.

1

1 Answers

3
votes

You can create a parent node(SKNode), add your spotlight nodes to this node as children, and then set this parent node as a maskNode to the single SKCropNode that contains the background as a child.