SKSpriteNode UserInteractionEnabled Not Working

2019-07-11 03:19发布

问题:

I've looked everywhere but nothing works. Thought I would just ask the question myself. I'm creating a little game in iOS 9 using SpriteKit. My game is going to have left and right controller buttons to move the player sprite. I add the SKSpriteNodes for the directional pads as follows

At the top of the main scene I put:

private var leftDirectionalPad = SKSpriteNode(imageNamed: "left")
private var rightDirectionalPad = SKSpriteNode(imageNamed: "right")

Then I run a method called prepareDirectionalPads

    func prepareDirectionalPads() {

    // left

    self.leftDirectionalPad.position.x = self.size.width * directionalPadLeftXPositionMultiplier
    self.leftDirectionalPad.position.y = self.size.height*directionalPadYPosition
    self.leftDirectionalPad.size.width = self.leftDirectionalPad.size.width/directionalPadSizeReductionMultiple
    self.leftDirectionalPad.size.height = self.leftDirectionalPad.size.height/directionalPadSizeReductionMultiple

    self.leftDirectionalPad.name = "leftDirectionalPad"
    self.leftDirectionalPad.alpha = directionalPadAlphaValue
    self.leftDirectionalPad.zPosition = 1
    self.addChild(leftDirectionalPad)
    self.leftDirectionalPad.userInteractionEnabled = true

    // right

    self.rightDirectionalPad.position.x = self.leftDirectionalPad.position.x*directionalPadSpacingMultiple
    self.rightDirectionalPad.position.y = self.size.height*directionalPadYPosition
    self.rightDirectionalPad.size.width = self.rightDirectionalPad.size.width/directionalPadSizeReductionMultiple
    self.rightDirectionalPad.size.height = self.rightDirectionalPad.size.height/directionalPadSizeReductionMultiple

    self.rightDirectionalPad.name = "rightDirectionalPad"
    self.rightDirectionalPad.alpha = directionalPadAlphaValue
    self.rightDirectionalPad.zPosition = 1
    self.addChild(rightDirectionalPad)
    self.rightDirectionalPad.userInteractionEnabled = true

}

I clearly set userInteractionEnabled for each SKSpriteNode to true. Then, in touches began I wrote...

override func touchesBegan(let touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */
        var touch = touches.first! as UITouch
        var location = touch.locationInView(self.view)
        var node = nodeAtPoint(location)

        print("Touched")
 }

Note: I have also tried var location = touch.locationInNode(self) but that doesnt work either.

I then run the app (either on my xCode Simulator or iPhone 6). If I touch on the SKNodes, nothing happens. Nothing is printed to the console. However, if I touch anywhere else on the screen, I get "touched" printed to the screen.

What am I doing wrong? I want to detect the touch on the pads so I can move the player sprite accordingly. It might be something really stupid I'm forgetting to do. Thanks for your time and patience. Much appreciated.

回答1:

Figured it out. So apparently self.leftDirectionalPad.userInteractionEnabled = true does not work. It needs to be self.leftDirectionalPad.userInteractionEnabled = false, which is very counter-intuitive. I don't get it but it works now. touchesBegan responds to when the user touches the SKSpriteNode.

    let touch = touches.first! as UITouch
    let location = touch.locationInNode(self)
    let node = nodeAtPoint(location)

    if(node.name == leftDirectionalPad.name)
    {
        print("left")
    }
    else if (node.name == rightDirectionalPad.name)
    {
         print("right")
    }