2015-02-24 20 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)