Raksha Saini
Raksha Saini

Reputation: 603

How to add button effects in swift SKSpriteNode

I have a Var Button.

var soundButton: SKSpriteNode! = nil

and the details is rest like.

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)

Than how can I add button effects like Button Color light when I click on it and make a sound also.

Upvotes: 0

Views: 539

Answers (1)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71862

One way to do it is you can change the image when button is selected.

like this way into your touchesBegan method:

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 you want to go to another scene with delay and you want transition you can use this code into your if statement:

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

Upvotes: 2

Related Questions