按下时将视觉效果添加到SKSpriteNode

时间:2015-02-24 14:54:35

标签: ios swift sprite-kit skspritenode

我正在使用Xcode 6中的第一个SpriteKit应用程序,在Swift中编码。现在我从透明的png文件中制作了一些漂亮的按钮。但是我试图在按下按钮时显示视觉效果。

示例我现在如何显示静态按钮:

let playButton = Button(imageNamed:"playButton")
playButton.position = CGPointMake(self.size.width/2, self.size.height/2 - playButton.size.height * 2.5 - displacement)
self.sharedInstance.addChildFadeIn(playButton, target: self)

任何效果都足够了,可能是脉冲效果,或按下时发光。我已经搜索过,但我在Swift中找不到任何东西。

编辑:更多信息

    class Button: SKSpriteNode {  
        init(imageNamed: String) {
            let texture = SKTexture(imageNamed: imageNamed)
            // have to call the designated initializer for SKSpriteNode
            super.init(texture: texture, color: nil, size: texture.size())
        }
        override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
            self.runAction(SKAction.scaleTo(1.3, duration: kButtonFadingSpeed))
        }    
        override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
            self.runAction(SKAction.scaleTo(1.3, duration: kButtonFadingSpeed))
        }
         override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
            self.runAction(SKAction.scaleTo(1.0, duration: kButtonFadingSpeed))
        }

        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
}

    func addChildFadeIn(node: SKNode, target: SKNode) {
        node.alpha = 0
        target.addChild(node)
        node.runAction(SKAction.fadeAlphaTo(1.0, duration: NSTimeInterval(kAddChildSpeed)))
    }

函数AddChildFadeIn在类中定义:singleton

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

我发现这个问题的一个很好的解决方案是复制原始节点,将副本的alpha设置为0.5,将其直接放在原始节点的顶部,并将其blendMode设置为add。这是一个样本。

// this is our original node that we want to make glow
playButton.anchorPoint = CGPointMake(0.5, 0.5)

// create a copy of our original node create the glow effect
let glowNode : SKSpriteNode = playButton.copy() as! SKSpriteNode
glowNode.size = playButton.size
glowNode.anchorPoint = playButton.anchorPoint
glowNode.position = CGPoint(x: 0, y: 0)
glowNode.alpha = 0.5
glowNode.blendMode = SKBlendMode.Add

// add the new node to the original node
playButton.addChild(glowNode)

// add the original node to the scene
self.addChild(playButton)

答案 1 :(得分:0)

您可能希望查看此线程How can you create a glow around a sprite via SKEffectNode,其中一些用户建议使用SKEffectNode。但是,这需要很多CPU功率。

还提议使用SKShapeNode,这也可能会出现性能问题,可以在此处进一步阅读Poor performance with SKShapeNode in Sprite Kit