Gam*_*mma 5 shadow ios sprite-kit sknode swift
这是我的设置,使用Sprite Kit.首先,我在SKScene中创建一个简单的精灵节点,如下所示:
let block = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(90, 160))
block.zPosition = 2
block.shadowCastBitMask = 1
addChild(block)
Run Code Online (Sandbox Code Playgroud)
然后向场景添加一个灯光节点:
let light = SKLightNode()
light.categoryBitMask = 1
light.falloff = 1
addChild(light)
Run Code Online (Sandbox Code Playgroud)
果然,块现在投下一个漂亮的小阴影:

现在我通过操纵其alpha值来淡化块,例如通过运行一个动作:
let fadeOut = SKAction.fadeAlphaTo(0.0, duration: 5.0)
block.runAction(fadeOut)
Run Code Online (Sandbox Code Playgroud)
这是一个尴尬的情况:当块变得越来越半透明时,阴影保持完全相同.这就是它在行动结束前的一刻:

一旦alpha完全降至0.0,阴影就会从一帧到下一帧突然消失.
这将是多漂亮,但有阴影慢慢的越来越弱,作为对象铸造它变得越来越透明.
使用Sprite Kit可以达到这样的效果吗?如果是这样,你会怎么做呢?
这有点棘手,因为 an 投射的阴影SKLightNode不受节点alpha属性的影响。您需要做的是shadowColor在SKLightNode淡出block.
基本步骤是:
shadowColor和该颜色的 Alpha 通道以供参考。SKAction.customActionWithDuration一个:
shadowColor为其原始颜色,但使用新的 Alpha 通道。例子:
let fadeDuration = 5.0 // We're going to use this a lot
// Grab the light's original shadowColor so we can use it later
let shadowColor = light.shadowColor
// Also grab its alpha channel so we don't have to do it each time
let shadowAlpha = CGColorGetAlpha(shadowColor.CGColor)
let fadeShadow = SKAction.customActionWithDuration(fadeDuration) {
// The first parameter here is the node this is running on.
// Ideally you'd use that to get the light, but I'm taking
// a shortcut and accessing it directly.
(_, time) -> Void in
// This is the original alpha channel of the shadow, adjusted
// for how much time has past while running the action so far
// It will go from shadowAlpha to 0.0 over fadeDuration
let alpha = shadowAlpha - (shadowAlpha * time / CGFloat(fadeDuration))
// Set the light's shadowColor to the original color, but replace
// its alpha channel our newly calculated one
light.shadowColor = shadowColor.colorWithAlphaComponent(alpha)
}
// Make the action to fade the block too; easy!
let fadeBlock = SKAction.fadeAlphaTo(0.0, duration: fadeDuration)
// Run the fadeBlock action and fadeShadow action in parallel
block.runAction(SKAction.group([fadeBlock, fadeShadow]))
Run Code Online (Sandbox Code Playgroud)