【问题标题】:Adding a visual effect to a SKSpriteNode when pressed按下时向 SKSpriteNode 添加视觉效果
【发布时间】:2015-02-24 14:54:35
【问题描述】:

我正在 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

非常感谢任何帮助!

【问题讨论】:

  • 什么是按钮? addChildFadeIn 在哪里定义?
  • 我已经使用该信息编辑了帖子。你能帮帮我吗?
  • 不幸的是我不知道 SpriteKit。我已经更新了你的标题和标签,让可以回答的人更容易发现你的问题。
  • 我能想到的最简单的效果就是用这个method给节点着色

标签: ios swift sprite-kit skspritenode


【解决方案1】:

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

// 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】:

    您可能想看看这个线程How can you create a glow around a sprite via SKEffectNode,其中一些用户建议使用 SKEffectNode。但是,这样做需要大量的 CPU 能力。

    还提出了 SKShapeNode 的用法,它也可能存在性能问题,可以在此处Poor performance with SKShapeNode in Sprite Kit 进一步阅读。

    【讨论】:

      猜你喜欢
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 2021-12-26
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多