Search code examples
swiftsprite-kitsknode

Change node.name inside of touch without running again?


I want to click on a certain spritenode and change the value of that spritenode. Here is the problem. When i do that, and set a if statement it knows to go to that if statement. However, i don't want it to run until next time the button is clicked.

Example:

apple.name = "apple"

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let node : SKNode = self.atPoint(location)
        if node.name == "apple" {
            appple.name = "orange" 
            print("Hello")
        }
        if node.name = "orange" {
        //do this
}

    }
}

Is there a way to do this?


Solution

  • first of all in your second if statement you are not checking the value "==" your are assigning a value "=" to it.

    anyway a simple solution to your problem is to add an else if statement.

    if node.name == "apple" {
            appple.name = "orange" 
            print("Hello")
        }else if node.name == "orange" {
        //do this
    }