2

I've been trying out the SKLightNode feature of SpriteKit and I'm having trouble manipulating shadows. Specifically, it appears the the falloff property of my SKLightNode doesn't do anything no matter what I set it to. Here is my code for the light:

//set up lights
var light = SKLightNode()
light.categoryBitMask = LightCategory.Light1
/*THIS DOESN'T DO ANYTHING*/ light.falloff = CGFloat(0.01)
light.ambientColor = UIColor.whiteColor()
light.lightColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.5)
light.shadowColor = UIColor(red: 0.5, green: 0.25, blue: 0.25, alpha: 0.5)
light.position = CGPointMake(size.width / 2.0, size.height * 0.75)
light.zPosition = DrawOrder.Lights
addChild(light)

And here is where I add it to the player:

//set up player
player.position = CGPoint(x: size.width * 0.1, y: size.height * 0.3)
player.shadowCastBitMask = LightCategory.Light1
player.zPosition = DrawOrder.Sprites
addChild(player)

According to Apple's documentation, falloff is supposed to set "the exponent for the rate of decay of the light source" and accepts a CGFloat from 0.0 to 1.0. But no matter what I set falloff to, the shadow is endless.

What am I doing wrong?

Updated with screenshot (FPS low due to simulator):

screenshot showing the issue

Floern
  • 33,559
  • 24
  • 104
  • 119
Derrick Hunt
  • 371
  • 3
  • 13

1 Answers1

2

I've had this problem before. As a work-around you can just set the falloff to a number larger than 1. In fact, if you create an SKLightNode in your SKScene's .sks file, it has a falloff of 1.5 by default so perhaps the documentation is incorrect?


Edit

In your case your problem also seems to be you've set the ambientColor to white. Set it to black and you'll be able to see the light working properly.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Just tested this. I can put literally any numeric value into .falloff without getting errors, even negative numbers. Still, nothing has any effect. – Derrick Hunt Apr 23 '15 at 16:01
  • Interesting! Setting `ambientColor` to `blackColor` does make the background lighting change, but not the shadow on the player (still goes on for infinity). Also, values from 0.0 to 1.0 have no effect on `falloff`, but values 2.0 - 5.0 do. At 5.0, the light goes completely dark. – Derrick Hunt Apr 23 '15 at 16:21
  • What do you mean 'goes on for infinity'? I'm getting the same results with regard to fall off. Possibly a bug with SpriteKit. – ABakerSmith Apr 23 '15 at 16:23
  • I updated my original post with a screenshot. I also noticed that `SKSpriteNodes` have a `shadowCastBitmask` (shown in the image) and a `shadowedBitmask` (which I tested achieving no results). What I don't understand now is why the shadow doesn't change length based on `falloff`. – Derrick Hunt Apr 23 '15 at 16:35
  • I may be wrong, but I'm pretty sure it's currently not possible to have shadows that don't extend forever using Sprite-Kit... Regarding using `shadowCastBitmask` and `shadowedBitmask`, take a look at this: http://stackoverflow.com/questions/28662600/sklightnode-cast-shadow-issue/29821325#29821325 – ABakerSmith Apr 23 '15 at 16:41
  • Ah, thank you. Looks like I'm just going to be better off making a drop shadow effect manually. – Derrick Hunt Apr 23 '15 at 16:43
  • Glad I could help. Sprite-Kit is still pretty new so hopefully these are things Apple intend to add in the future. Good luck with your project! – ABakerSmith Apr 23 '15 at 16:45