Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
            }

    }

}