E.O'Brien
E.O'Brien

Reputation: 13

SKSpriteNode touch detected swift

I am having trouble with detecting a specific node being touched. Here is what i have to far.

let playagain = SKSpriteNode(imageNamed: "PlayAgain.png")

override func didMoveToView(view: SKView) {
    super.didMoveToView(view)

}

then when the player dies these two nodes come up.

    playagain.position = CGPoint(x:frame.size.width * 0.5, y: frame.size.height * 0.5)
    addChild(playagain)

    gameover.position = CGPoint(x:frame.size.width * 0.5, y: frame.size.height * 0.75)
    addChild(gameover)

everything above works. the node comes on the screen where i asked i just can't seem to get it to show i clicked it. as you can see the node is called playagain, when the playagain node is clicked i want to be able to refresh the game. what i have so far is below.

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

    for touch in touches {
       let location = (touch as! UITouch).locationInNode(self)
       let play = self.nodeAtPoint(location)
       if play.name == "playagain" {

           println("touched")
       }
    }
}

thanks!

Upvotes: 1

Views: 1519

Answers (1)

crashoverride777
crashoverride777

Reputation: 10674

Are you not using the latest Xcode? Your touches began code should not run with swift 2 and Xcode 7.

Try this

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)

        if playagain.containsPoint(location) {

         /// playagain was pressed, do something
        }
    }
}

or this

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(location)

        if touchedNode == playagain {

         /// playagain was pressed, do something
        }
    }
}

Upvotes: 1

Related Questions