1

I have a several SKSpriteNodes in my SpriteKit game like backgrounds for pause and game over scenes and buttons for it. For now I'm declaring and adding them in didMoveToView and using things like:

let pauseButton = SKSpriteNode(imageNamed: "pauseButton")
let pauseBackground = SKSpriteNode (imageNamed: "pauseBackground")
let resumeButton = SKSpriteNode (imageNamed: "resumeButton")

Then, I'm initializing them:

self.pauseButton.anchorPoint = CGPointMake(0.5, 0.5)
self.pauseButton.position = CGPointMake(CGRectGetWidth(self.frame) - self.cookieSpot / 3, CGRectGetMaxY(self.frame) - self.cookieSpot / 3)
pauseButton.size = self.pauseButton.size
pauseButton.name = "pauseButton"
pauseButton.zPosition = 1000

pauseBackground.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
self.pauseBackground.hidden = true
self.pauseBackground.name = "pauseBackground"
self.pauseBackground.zPosition = +1
resumeButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) + self.resumeButton.size.height * 2 + self.resumeButton.size.height - self.resumeButton.size.height / 4)
self.resumeButton.hidden = true
self.resumeButton.name = "resumeButton"
self.resumeButton.zPosition = +1

And then adding them:

self.addChild(pauseBackground)
self.addChild(resumeButton)
self.addChild(gameOverBackground)

when it's not needed and when user is tapping a pause button I'm trying to use things like this:

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

   }
}

That's not working for me at all. They are spawning I guess, but they are invisible. How one should store that nodes when they're not needed and spawn them when it's the right time?

Burundanga
  • 668
  • 5
  • 15
  • What do you mean by "spawning" ? – sangony Apr 02 '15 at 00:48
  • That should work. Maybe its something with initialization of the pause button? post it for us – MaxKargin Apr 02 '15 at 00:50
  • @sangony I mean to present them above other nodes. Think of a pause scene that appearing on a screen when when user tap a button for example. For now I'm declaring it in a class, position them and do `addChild` in `didMoveToView` and set them to be hidden. Then, when user interacts with pause button I want them to appear above other nodes. I try `hidden = false` but they are still invisible. Although I can unpause the game if I'm clicking the spot where resume button should be. @M321K I've updated my code now! – Burundanga Apr 02 '15 at 11:05

1 Answers1

3

Adding nodes is done at the end of runloop and you are pausing the view before that...I guess the problem is related to that (tested it and I got same results). In Obj-C an easy workaround would be using the performSelector method like described here. In Swift it get trickier an you should probably use something like dispatch_after or go with something like this ( using NSTimer ). Hope this helps a bit. Also this is not an definitive answer, and probably there are more solutions for the same problem.

EDIT:

I just found workable solution ( pausing is moved inside some method ):

func pauseGame()
   {
         self.scene?.view?.paused = true
   }

And then call it in touchesBegan

 self.runAction(SKAction.runBlock(self.pauseGame))

Your touchesBegan should probably look like this:

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            switch self.nodeAtPoint(location) {
            case pauseButton:
                self.pauseBackground.hidden = false
                self.resumeButton.hidden = false

               self.runAction(SKAction.runBlock(self.pauseGame))
            case resumeButton:
                self.pauseBackground.hidden = true
                self.resumeButton.hidden = true
                self.scene?.view?.paused = false
            default:
                println("not good")

            }
        }
    }
Community
  • 1
  • 1
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • Well, that's great! Let me make it clear if I understood you right. Do you suggest to call pause SKAction after `self.pauseBackground.hidden = false` `self.resumeButton.hidden = false` in my switch statement? Or I should rewrite all touchesBegan function? – Burundanga Apr 02 '15 at 11:41
  • Just try to call self.runAction(SKAction.runBlock(self.pauseGame)) instead of self.scene?.view?.paused = true ... It should work(works for me). – Whirlwind Apr 02 '15 at 11:45
  • Yes! It totally worked! I Thank you @Whirlwind! At last I can go further with it :D – Burundanga Apr 02 '15 at 14:29