简体   繁体   English

如何在SpriteKit和SceneKit中暂停游戏-SWIFT

[英]How can I pause a game in SpriteKit and SceneKit - SWIFT

I'm trying to pause my game by touching a button but I can't get it to work. 我试图通过触摸按钮来暂停游戏,但无法正常工作。

I tried this : 我尝试了这个:

pause = SKSpriteNode (imageNamed: "pause.png")
pause.size = CGSizeMake(30, 30)
pause.position = CGPointMake(30, 30)
self.addChild(pause)

and I'm trying to call a function like this: 我正在尝试调用这样的函数:

func touchesBegin (touches: NSSet!, withEvent event: UIEvent!)   
{

    for touch: AnyObject in touches
    {
        let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.pause
        {
            let skView = self.view as SKView
            skView.paused = true
        }
    }

}

When I touch the pause button nothing happens... what's wrong? 当我触摸暂停按钮时,什么都没有发生……怎么了?

Thank you! 谢谢!

I resolved by myself the problem : 我自己解决了这个问题:

here my code function : 这是我的代码功能:

  override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) 
  {
    var touch:UITouch = touches.anyObject() as UITouch
    pauseText.text = "Pause"
    pauseText.fontSize = 50
    pauseText.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)

                   /* bouton play/pause */

    var locationPause: CGPoint = touch.locationInNode(self)
    if self.nodeAtPoint(locationPause) == self.pause
        {
        addChild(pauseText) // add the text
        pause.removeFromParent ()  // to avoid error when you touch again
        self.runAction (SKAction.runBlock(self.pauseGame))
        }

  }

To resume the game you just have to add this code before the last "}" : 要继续游戏,您只需在最后一个“}”之前添加以下代码:

    if self.nodeAtPoint(locationPause) == self.pauseText
        {
        pauseText.removeFromParent() // remove the pause text
        self.view.paused = false // resume the game
        addChild(pause) // add the pause button
        }

And in SKScene subclass add this function to add a label during the pause : 并在SKScene子类中添加此函数以在暂停期间添加标签:

func pauseGame()
    { 
    self.view.paused = true // to pause the game
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM