Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Button animation Spritekit with Swift

Hi, I'm trying to find a way so that when a user touches a button it switches to a new SKSpriteNode and when he releases his finger the SKSpriteNode switches to a third one. With my code it is only working one way. How can I continue doing this?

Thanks

This is the code I have for the moment:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.noAdsButton {
            noAdsButton.hidden = true
            noAds1Button.hidden = false
        }

        if self.nodeAtPoint(location) == self.rankingButton {
            rankingButton.hidden = true
            ranking1Button.hidden = false
        }

        if self.nodeAtPoint(location) == self.musicOnButton {
            musicOnButton.hidden = true
            musicOff1Button.hidden = false
            musicOffButton.hidden = true
        }

        if self.nodeAtPoint(location) == self.soundOnButton {
            soundOnButton.hidden = true
            soundOff1Button.hidden = false
            soundOffButton.hidden = true
        }
    }
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches{
        let location = touch.locationInNode(self)

        if self.nodeAtPoint(location) == self.noAdsButton {
            noAdsButton.hidden = false
            noAds1Button.hidden = true
        }

        if self.nodeAtPoint(location) == self.rankingButton {
            rankingButton.hidden = false
            ranking1Button.hidden = true
        }

        if self.nodeAtPoint(location) == self.musicOnButton {
            musicOnButton.hidden = false
            musicOff1Button.hidden = true
            musicOffButton.hidden = true
        }

        if self.nodeAtPoint(location) == self.soundOnButton {
            soundOnButton.hidden = false
            soundOff1Button.hidden = true
            soundOffButton.hidden = true
        }
    }
}

   override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

        for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
            if self.nodeAtPoint(location) == self.noAdsButton {
                noAdsButton.hidden = false
                noAds1Button.hidden = true
            }

            if self.nodeAtPoint(location) == self.rankingButton {
                rankingButton.hidden = false
                ranking1Button.hidden = true
            }

            if self.nodeAtPoint(location) == self.musicOnButton {
                musicOnButton.hidden = true
                musicOff1Button.hidden = true
                musicOffButton.hidden = false
                audioPlayer.stop()
            }

            if self.nodeAtPoint(location) == self.soundOnButton {
                soundOnButton.hidden = true
                soundOff1Button.hidden = true
                soundOffButton.hidden = false
            }

    }

}