Adding a visual effect to SKSpriteNode when clicked

I am working on my first SpriteKit application in Xcode 6, coding in Swift. Now I have made some nice buttons from transparent png files. But I'm trying to show a visual effect when a button is pressed.

An example, as I now show a static button:

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) 

Any effect will be sufficient, possibly a pulsed effect or a glow when pressed. I searched, but I can not find anything in Swift.

edit: more details

  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))) } 

The AddChildFadeIn function AddChildFadeIn defined in the class: singleton

Any help is much appreciated!

+5
source share
2 answers

I found a good solution to this problem, to copy the source node, install a copy of alpha 0.5 to place it directly above the original node, and set its blendMode to be added. here is a sample.

 // 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
source

You can take a look at this thread. How to create a glow around a sprite through SKEffectNode , where some users suggested using SKEffectNode. However, this requires a lot of processor power.

There is also a use of the proposed SKShapeNode, which may also have performance problems, and further reading about it can be done here. Poor performance with SKShapeNode in the sprite set .

0
source

Source: https://habr.com/ru/post/1214085/


All Articles