Matt Davidson
Matt Davidson

Reputation: 13

Issue with recognising touch in SKLabelNode

I have been programming in Swift for about four months now using Sprite Kit to build some simple Arcade games for IOS. Until recently I haven't had any problems with recognising touches in specific nodes. In the main screen in one of my projects, I have added another SKLabelNode for the latest addition to the app, following the same layout of implementation as the others, but this one doesn't work. When the label is tapped it is supposed run a function but doesn't even get that far, I figured out using breakpoints. Here is all of the relevant code, please have a look, I have going over this four hours and it has been driving me crazy.

import SpriteKit

let twistedLabelName = "twisted"

class StartScene: SKScene {

var play1P = SKLabelNode(fontNamed: "HelveticaNeue-Thin")
var play2P = SKLabelNode(fontNamed: "HelveticaNeue-Thin")
var playTwisted = SKLabelNode(fontNamed: "HelveticaNeue-Thin")

override func didMoveToView(view: SKView) {
    initializeValues()
    self.userInteractionEnabled = true
}

func initializeValues () {
    backgroundColor = UIColor.whiteColor()


    play1P.name = "1p"
    play1P.text = "Play 1P"
    play1P.fontColor = SKColor.blackColor()
    play1P.fontSize = 40
    play1P.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetHeight(self.frame) * 0.8)
    play1P.zPosition = 100

    self.addChild(play1P)


    play2P.name = "2p"
    play2P.text = "Play 2P"
    play2P.fontColor = SKColor.blackColor()
    play2P.fontSize = 40
    play2P.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetHeight(self.frame) * 0.6)
    play2P.zPosition = 100

    self.addChild(play2P)

    playTwisted.text = "Twisted"
    playTwisted.fontColor = SKColor.blackColor()
    playTwisted.name = twistedLabelName
    playTwisted.fontSize = 40
    playTwisted.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetHeight(self.frame) * 0.4)

    self.addChild(playTwisted)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch in touches {
        let location = touch.locationInNode(self)
        if let theName = self.nodeAtPoint(location).name {
            if theName == "1p" {
                // Some function
            }
            else if theName == "2p" {
                // Some function
            }
            else if theName == twistedLabelName {
                // This is the one that doesn't work
                // Some function
            }
        }
    }
   }
}

Upvotes: 0

Views: 1152

Answers (1)

Bobjt
Bobjt

Reputation: 4100

Note that you can extend any SKNode (including SKLabelNode) and handle touches in the subclass. Set isUserInteractionEnabled = true first though. e.g.

import SpriteKit

class MyLabelNode: SKLabelNode {
    override init() {
        super.init()
        isUserInteractionEnabled = true
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:  aDecoder)
        isUserInteractionEnabled = true
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print( #file + #function )
    }
}

Upvotes: 2

Related Questions