AaronChapmanDev
AaronChapmanDev

Reputation: 124

False if Statement Still Executes

I am using the Xcode 6 Beta, and I have used this exact code in another class, setup up in the same way. For some reason it doesn't want to work here. When I touch an SKNode, I grab its name, and compare the name to two strings, if it matches either of them, I execute some code. See below.

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let node: SKNode = self.nodeAtPoint(touch.locationInNode(self))

        if node.name == "body" || "face" {
            self.childNodeWithName("face").runAction(SKAction.rotateByAngle(6.283, duration: 0.75))
        }
    }
}

As I said before, this exact code work flawlessly everywhere else I have used it, but no matter which node I press, the code inside the if statement will be run. Yes, I have printed out the names of nodes I am touching, and they do not match. Any ideas?

Upvotes: 0

Views: 137

Answers (3)

GoZoner
GoZoner

Reputation: 70165

Your if is idiomatically expressed as a switch with

switch node.name {
  case "body", "face":
    self.childNodeWithName ...
  default: break
}

Upvotes: 2

codester
codester

Reputation: 37189

I think it should

if node.name == "body" || node.name == "face" { //change this statement

replace this in your function

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let node: SKNode = self.nodeAtPoint(touch.locationInNode(self))

        if node.name == "body" || node.name == "face" {  //change this statement
            self.childNodeWithName("face").runAction(SKAction.rotateByAngle(6.283, duration: 0.75))
        }
    }
}

Upvotes: 1

slazyk
slazyk

Reputation: 1792

You probably meant

if node.name == "body" || node.name == "face" {

It would be more clear with parentheses, you are doing:

if (node.name == "body") || ("face") {

And "face" is true, which probably is a bug on Apple's side as String does not conform to LogicValue so you should not be able to test it like that. Probably the literal gets interpreted as something else conforming to LogicValue e.g. a CString.

Upvotes: 3

Related Questions