2015-02-24 63 views
5

我正在使用Swift编码Xcode 6中的第一个SpriteKit应用程序。现在我已经通过透明png文件制作了一些不错的按钮。但是我试图在按下按钮时显示视觉效果。在按下时向SKSpriteNode添加视觉效果

例子我怎么现在显示静态按钮:

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

任何帮助,非常感谢!

+0

什么是Button? addChildFadeIn定义在哪里? – 2015-02-24 15:05:31

+0

我已经使用该信息编辑了帖子。你可以帮帮我吗? – 2015-02-24 17:53:11

+0

不幸的是我不知道SpriteKit。我更新了您的标题和标签,以便您的问题能够被可以回答的人更容易发现。 – 2015-02-24 17:59:05

回答

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)