0

I'm trying to make a button that's an SKLabelNode. When it gets pressed, it's supposed to change scenes, but there is something wrong with the line where location is declared.

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)

    let location = touches.locationInNode(self)
    let touchedNode = self.nodeAtPoint(location)

    if touchedNode.name == "startGameButton" {
        let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)

        let scene = GameScene(size: self.scene.size)
        scene.scaleMode = SKSceneScaleMode.AspectFill

        self.scene.view.presentScene(scene, transition: transition)
    }
}

The error is here on the line.

let location = touches.locationInNode(self)

It reads

'Set< NSObject>' does not have a member named 'locationInNode'

I'm not sure how to fix it. I've looked at a lot of working button templates, but mine always has an error.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
pizzafoot
  • 125
  • 1
  • 1
  • 8
  • Google "Set< NSObject>' does not have a member named 'locationInNode" – sangony May 30 '15 at 22:16
  • You can also try this Sprite kit button, it works like a native UIButton from UIKit https://github.com/rogermolas/RMSpriteButton/blob/master/RMSpriteButton/RMSpriteButton.swift – Roger Jul 13 '16 at 04:23

2 Answers2

1

The problem is exactly what the error states - Set<NSObject> doesn't have a method named locationInNode. What you need to do is retrieve an object out of the Set; check it's a UITouch object; if it is, you can use it to get a touch location. Try:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let location = (touches.first as? UITouch)?.locationInNode(self) {
        // ...
    }
}

Or

if let touch = touches.first as? UITouch {
    let location = touch.locationInNode(self)
    // ...
}
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
0

To fix it, this is the default fix, it just enumerates through all the touches.

for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        node = self.nodeAtPoint(location)
        //do something
}
Wraithseeker
  • 1,884
  • 2
  • 19
  • 34