Burundanga
Burundanga

Reputation: 668

Why SKNode isn't reacting to touch in SpriteKit game?

In order to make a pause in a SpriteKit game I made a pause button. It worker like a charm when I wanted to make an another scene for pause and respond to touch was transitioning to a scene. My function then looked like this (P.S. pause = pause button) But there were other problems with it and I decided to make pause within my main scene: playscene So I added a pauseBackground and resumeButton that is attached to pause background and hided it (declared resumeButton.hidden = true) Now my function looks like this:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    if self.onGround {
        self.speedY = -20.0
        self.onGround = false
    }
    for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.pause {
        self.scene?.view?.paused = true
        self.pauseBackground.hidden = false
        self.resumeButton.hidden = false
        if self.nodeAtPoint(location) == self.resumeButton {
        self.pauseBackground.hidden = true
        self.resumeButton.hidden = true
        self.scene?.view?.paused = false
            }
        }
    }
}

It's interesting because it worked with scene transitions, but now Xcode can't detect or ignore my tap in pause button location. I don't really know why. Could you please help me? Update: I tried switch statement:

for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
        switch self.nodeAtPoint(location) {
        case self.pause:
        self.scene?.view?.paused = true
        self.pauseBackground.hidden = false
        self.resumeButton.hidden = false
        case self.resumeButton:
        self.pauseBackground.hidden = true
        self.resumeButton.hidden = true
        self.scene?.view?.paused = false
        default:
        println()

    }
}

Sadly, it's not working either! I really have no idea why it's not detecting a touch in that location.

Upvotes: 1

Views: 1019

Answers (1)

suyama
suyama

Reputation: 262

  1. Give a name to your pause button. Control if your pause button is touched in touchesBegan by printing the name of the touched node.

  2. Control the zPosition of the touched node and the pause button. Set the zPosition of the pause button = 1000 and try touching it.

EDIT:

enter image description here

Let's say, the node node1 is the parent node of the pauseButton.

node1.zPosition = 100.0f;
pauseButton.zPosition = 200.0f;

the continueButton is above the scene

continueButton.zPosition = 250.0f;

That means, that the zPosition of the pauseButton is 200 above the parent node node1. The zPosition of the continueButton (250) is less than the zPosition of the pauseButton above the scene, although the zPosition seems to be less (200). That's because the zPosition of the pauseButton is 300 above the scene. The zPosition of the parent node is very important to control the zPositions of its children and the other nodes of the scene.

zPosition of a node is always relative to its parent node. Have your both buttons (pause and resume button) the same parent node? The best way to control the touched node, is to give each node a name and a zPosition, and to print this (name and zPosition with NSLog() / println()) in the touchesBegan. With this you know exactly which node is touched and you know its zPosition. And so you can correct the zPosition of each node, so that it works as you need.

Upvotes: 1

Related Questions