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

时间:2023-01-22 22:44:49

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

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

Example how 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 would be enough, maybe a pulse effect, or glow on press. I've searched, but I can't really find anything in Swift.

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

edit: more info

编辑:更多信息

    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 function AddChildFadeIn is defined in class: singleton

AddChildFadeIn函数在类中定义:singleton

Any help is much appreciated!

任何帮助深表感谢!

2 个解决方案

#1


1  

I found a nice solution to this problem is to copy the original node, set the copy's alpha to 0.5 place it directly over top of the original node, and set its blendMode to add. here is a sample.

我发现这个问题的一个很好的解决方案是复制原始节点,将副本的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)

#2


0  

You might want to take a look at this thread How can you create a glow around a sprite via SKEffectNode where some users have proposed the use of SKEffectNode. However, it takes alot of CPU power to do that.

您可能想看看这个线程如何通过SKEffectNode在sprite周围创建一个发光,其中一些用户建议使用SKEffectNode。但是,这需要很多CPU功率。

There is also the usage of SKShapeNode proposed which also might have performance issues and further reading about it can be done here Poor performance with SKShapeNode in Sprite Kit.

还有一些SKShapeNode的使用,它也可能存在性能问题,可以在这里进一步阅读它。在Sprite Kit中使用SKShapeNode表现不佳。

#1


1  

I found a nice solution to this problem is to copy the original node, set the copy's alpha to 0.5 place it directly over top of the original node, and set its blendMode to add. here is a sample.

我发现这个问题的一个很好的解决方案是复制原始节点,将副本的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)

#2


0  

You might want to take a look at this thread How can you create a glow around a sprite via SKEffectNode where some users have proposed the use of SKEffectNode. However, it takes alot of CPU power to do that.

您可能想看看这个线程如何通过SKEffectNode在sprite周围创建一个发光,其中一些用户建议使用SKEffectNode。但是,这需要很多CPU功率。

There is also the usage of SKShapeNode proposed which also might have performance issues and further reading about it can be done here Poor performance with SKShapeNode in Sprite Kit.

还有一些SKShapeNode的使用,它也可能存在性能问题,可以在这里进一步阅读它。在Sprite Kit中使用SKShapeNode表现不佳。