1

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.

Burundanga
  • 668
  • 5
  • 15

1 Answers1

1
  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.

suyama
  • 262
  • 2
  • 15
  • wow, it worked! Could you explain why? I've never ever sat zPosition to my play scene. And why 1000? After that I tried to set pause button zPosition to 4 and 500 and it didn't work! So in order to show a `pauseBackground` and `resumeButton` to a player I should make their zPositions like = 1001? – Burundanga Mar 31 '15 at 17:33
  • I mean my `pauseButton` worked and scene is pausing. But It's not showing my `pauseBackground` and `resumeButton` nodes. However, when I'm clicking the spot where `resumeButton` should appear – game is unpaused. – Burundanga Mar 31 '15 at 18:29
  • zPositions are important for ordering the nodes form back to front. Please read: **Understanding the Drawing Order for a Node Tree** in the apple documentation: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html. I always set: `view.ignoresSiblingOrder = YES`, and set the zPosition for every node. So I have never a problem with the ordering of the nodes or touching it. If this solves your problem, please accept my answer. Thank you. – suyama Mar 31 '15 at 19:27
  • Yes, I understand what zPosition does and set `ignoresSiblingOrder` to true. I'm just curious why your suggest 1000 because it worked but other numbers don't. Nevertheless, thank you very much! I don't understand it yet, but it already kind of works :D Thanks! – Burundanga Apr 01 '15 at 18:07
  • I read that page of documentation, but I still can't understand why `pauseButton` is working but my resume button and pause background are invisible! I tried many zPosition numbers for them (`= +1` too). Also, if I'm trying to set their zPositions to a number that is higher than pauseButton number 1000 – pauseButton stops working. – Burundanga Apr 01 '15 at 19:32