Diego Benítez Diego Benítez - 1 year ago 74
iOS Question

How to change the player color by pressing a button?

I want to create a button and if I press it, the color of the player change to the same color of the button.

The problem is that when I tap the screen it plays the action, and I only want to play the action if the player press the button(in this case another


class GameScene: SKScene, SKPhysicsContactDelegate {

var circuloPrincipal = SKSpriteNode(imageNamed: "circulo")

var colorVerde: UIColor = UIColor(red: 0.3, green: 0.7, blue: 0.5, alpha: 0.9)

var circuloVerde = SKSpriteNode(imageNamed: "fondoBaseBlanco.png")

for touch in touches {
let location = circuloVerde

let circuloVer = circuloVerde

circuloVerde.position = circuloVerde.position

circuloPrincipal.color = colorVerde

//circulo principal = player
//color verde = green color
//circulo verde = button


Answer Source

When you create your button add a name like this:

circuloVerde.name = "button"

In the touchesBegan delegate method do:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        /* Called when a touch begins */

        let touch = touches.first
        let positionInScene = touch!.locationInNode(self)

        let touchedNode = self.nodeAtPoint(positionInScene)

        if let name = touchedNode.name
           print("name : \(name)")
           if name == "button" {
              //do my stuff 
              circuloPrincipal.color = colorVerde
              // or you can do also
              circuloPrincipal.color = circuloVerde.color 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download