[swift]暂停菜单层

标签: ios Swift
发布时间: 2017/3/6 23:29:23
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

暂停菜单(比如这个),但如果我使用相同的图层,我不能交互场景时它已经暂停 (我猜? !)。我到处都看为"暂停菜单",据说人们使用另一个层。我试着做万次,但它不工作 (或也许我并不知道的人) = (

这就是我所做的到目前为止︰

**Declaring**

var gameLayer = SKNode()
var pauseLayer = SKNode()

var gameStarted = Bool()

var Ground = SKSpriteNode()
var Character = SKSpriteNode()

var pauseButton = SKSpriteNode()
var playButton = SKSpriteNode()


**Setup functions**

func setupGround(){

    //Ground
    Ground = SKSpriteNode(imageNamed: "Ground")
    Ground.setScale(0.5)
    Ground.position = CGPoint(x: self.frame.width / 2, y: 0)
    Ground.zPosition = 1

    Ground.physicsBody = SKPhysicsBody(rectangleOfSize: Ground.size)
    Ground.physicsBody?.categoryBitMask = PhysicsCategory.Ground
    Ground.physicsBody?.collisionBitMask = PhysicsCategory.Character
    Ground.physicsBody?.contactTestBitMask = PhysicsCategory.Character
    Ground.physicsBody?.affectedByGravity = false
    Ground.physicsBody?.dynamic = false
}
func setupCharacter(){

    //Character
    Character = SKSpriteNode(imageNamed: "Character")
    Character.size = CGSize (width: 60, height: 40)
    Character.position = CGPoint(x: self.frame.width / 2 - Character.frame.width - 100, y: self.frame.height / 2)
    Character.zPosition = 2

    Character.physicsBody = SKPhysicsBody(circleOfRadius: Character.frame.height / 2.5)
    Character.physicsBody?.categoryBitMask = PhysicsCategory.Character
    Character.physicsBody?.collisionBitMask = PhysicsCategory.Ground
    Character.physicsBody?.contactTestBitMask = PhysicsCategory.Ground
    Character.physicsBody?.affectedByGravity = false
    Character.physicsBody?.dynamic = true
    Character.physicsBody?.allowsRotation = false
}

func setupPauseButton (){

    //Pause
    pauseButton = SKSpriteNode (imageNamed: "pause")
    pauseButton.setScale(0.25)
    pauseButton.position = CGPoint(x: self.frame.width / 10, y: self.frame.height / 1.5)
    //pauseButton.zPosition = 3
}
func setupPlayButton(){

    //Play
    playButton = SKSpriteNode (imageNamed: "play")
    playButton.setScale(0.15)
    playButton.position = CGPoint(x: self.frame.width / 10, y: self.frame.height / 1.5)
    //playButton.zPosition = 3
}


**Creating scene**

func createScene(){

    self.physicsWorld.contactDelegate = self

    setupGround()
    gameLayer.addChild(Ground)

    setupCharacter()
    gameLayer.addChild(Character)

    setupPauseButton()
    gameLayer.addChild(pauseButton) //add pauseButton to gameLayer
}

func createPauseLayer(){

    setupPlayButton()
    pauseLayer.addChild(playButton) //add playButton to pauseLayer
}

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    self.addChild(gameLayer)

    createScene()
    createPauseLayer()
}


**Touches began**

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

    //when touch buttons/screen
    for touch in touches {

        let location = touch.locationInNode(self)
        let node = nodeAtPoint(location)

        if node == pauseButton{
            pauseState()
        }

        else if node == playButton{
            playState()
        }

        else {

            if gameStarted == false{  //game didn't start yet

                gameStarted = true

                Character.physicsBody?.affectedByGravity = true //start falling when touch the screen

                //first jump
                Character.physicsBody?.velocity = CGVectorMake(0, 0)
                Character.physicsBody?.applyImpulse(CGVectorMake(0, 15))

                //first sound effect
                //self.runAction(SKAction.playSoundFileNamed("fly.mp3", waitForCompletion: false))
            }
            else{

                //jump
                Character.physicsBody?.velocity = CGVectorMake(0, 0)
                Character.physicsBody?.applyImpulse(CGVectorMake(0, 15))

                //sound effect
                //self.runAction(SKAction.playSoundFileNamed("fly.mp3", waitForCompletion: false))
            }
        }
    }
}

//states
func pauseState(){

    pauseButton.hidden = true //hide pauseButton
    gameLayer.paused = true //pause gameLayer
    self.addChild(pauseLayer) //add pauseLayer
}
func playState(){

    pauseButton.hidden = false //show pauseButton
    pauseLayer.removeFromParent() //remove pauseLayer
    gameLayer.paused = false //unpause gameLayer
}

解决方法 1:

我不会暂停的场景,您应该创建您像这样的游戏设置

SKScene
— — GameNode
---所有其他游戏相关节点
— — OverlayNode
---你暂停节点

你有除了暂停层的所有节点都进入 GameNode 所有暂停节点进入覆盖节点

当你准备要暂停你的场景时,你实际上暂停您的 GameNode。

这可以使你的场景还活着,而你的游戏停顿了一下,所以你可以使用暂停按钮。

当你播放声音时,发挥他们对在 GameNode,未到场的节点,这样当你暂停 GameNode,声音自动暂停

class GameScene
{
  let pauseLayer = SKNode()
  let gameLayer = SKNode()
  func didMoveToView(view:SKView)
  {
    self.addChild(gameLayer)
  }

  func pauseButton()
  {
    gameLayer.paused = true
    self.physicsWorld.speed = 0;
    self.addChild(pauseLayer)
  }

  func unpauseButton()
  {
    pauseLayer.removeFromParent()
    self.physicsWorld.speed = 1;
    gameLayer.paused = false
  }
}
赞助商