如何在swift SKSpriteNode中添加按钮效果

时间:2015-05-13 05:57:41

标签: swift button skspritenode

我有一个Var Button。

var soundButton: SKSpriteNode! = nil

,细节就像休息一样。

soundButton           =   SKSpriteNode(imageNamed: "\(soundImg)")
soundButton.position  =   CGPoint(x: frame.size.width*0.08 , y:frame.size.width*0.08);
soundButton.size      =   CGSizeMake(frame.size.width*0.10,frame.size.width*0.10 )
self.addChild(soundButton)

如果我点击它并发出声音,我怎么能添加按钮效果,如按钮颜色灯。

1 个答案:

答案 0 :(得分:2)

一种方法是在选择按钮时更改图像。

像这样进入touchesBegan方法:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)

        if self.nodeAtPoint(location) == self. soundButton {
            soundButton.texture = SKTexture(imageNamed: "soundSelected")

        }
    }
}

如果您想延迟转到另一个场景,并且想要转换,可以将此代码用于if声明:

let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let letsPlay = playScene(size: self.size)
self.view?.presentScene(letsPlay, transition: reveal)