Josh Schlabach
Josh Schlabach

Reputation: 411

How can I make images that act as buttons in Swift?

I have the code for one button on my scene, but I tried using that same code to make another button in the same scene, and it didn't work. Here's the code I have so far:

for touch:AnyObject in touches {
        let location = touch.locationInNode(self)
        if playGameButton.containsPoint(location) {
            let newScene = GameplayScene(size: self.size)
            newScene.scaleMode = scaleMode
            self.view?.presentScene(newScene)

}
    } 

Again, that's only one button. When I use that same bit of code for another image, it doesn't work.

Upvotes: 1

Views: 54

Answers (1)

tylersimko
tylersimko

Reputation: 313

A better way to approach this problem would be to use an SKSpriteNode object as your button and set its name property to easily distinguish between multiple buttons. For example, if you have an SKSpriteNode object button1:

button1.name = "first"

Then in your touch method to check which was touched, you can use the name property:

SKNode node = self.nodeAtPoint(location)

if node.name == "first" {
// do something
}

Upvotes: 3

Related Questions