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!
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

Sabrina Polakoff
2,767 PointsButton 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) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
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.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.soundOnButton {
soundOnButton.hidden = true
soundOff1Button.hidden = true
soundOffButton.hidden = false
}
}
}
5 Answers

Michael Park
5,719 Pointsif you want the buttons to change when there is a touch and release why do you have the "touchesMoved" function?

Sabrina Polakoff
2,767 PointsThe button should change when it is tapped a second time. The code I have now is only for a single tap because I don't know how to reverse the action for the second tap.
It should look like this button: https://www.youtube.com/watch?v=ucrlUp01exE

Alexandre Saleh
2,406 PointsI am having this same problem and i cant find an answer anywhere...

Alexandre Saleh
2,406 PointsI have found something that looks like an answer, but i can't seem to move from scene i am on Gamescene and i want to move to my InGameScene.
import SpriteKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) { / /* Setup your scene here */
class startButtonAnimationAction: SKNode {
var buttonNotPressed: SKSpriteNode
var buttonPressed: SKSpriteNode
var action: () -> Void
init(buttonNotPressedImage: String, buttonPressedImage: String, buttonAction: () -> Void) {
buttonNotPressed = SKSpriteNode(imageNamed: "startButtonNotPressed")
buttonPressed = SKSpriteNode(imageNamed: "startButtonPressed")
buttonPressed.hidden = true
action = buttonAction
super.init()
userInteractionEnabled = true
addChild(buttonNotPressed)
addChild(buttonPressed)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/ /* Called when a touch begins */
buttonPressed.hidden = false
buttonNotPressed.hidden = true
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
var touch: UITouch = touches.allObjects[0] as UITouch
var location: CGPoint = touch.locationInNode(self)
if buttonNotPressed.containsPoint(location) {
buttonPressed.hidden = false
buttonNotPressed.hidden = true
} else {
buttonPressed.hidden = true
buttonNotPressed.hidden = false
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
var touch: UITouch = touches.allObjects[0] as UITouch
var location: CGPoint = touch.locationInNode(self)
if buttonNotPressed.containsPoint(location) {
}
buttonPressed.hidden = true
buttonNotPressed.hidden = false
}
}
var startButton: startButtonAnimationAction = startButtonAnimationAction(buttonNotPressedImage: "startButtonNotPressed", buttonPressedImage: "startButtonPressed", buttonAction: goToPlayScene)
startButton.position = CGPointMake(self.frame.width / 2, self.frame.height / 2 )
addChild(startButton)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered vergeet override niet voor func te zetten*/
}
}

guyvenson cadet
361 Pointswhy hasn't treehouse made a Spikekit course for swift already? swift been out for about 2yrs now.