暂停菜单层

时间:2016-04-07 00:26:52

标签: ios swift sprite-kit

我正在尝试制作暂停菜单(例如this),但如果我使用相同的图层,则暂停时我无法与场景交互(我猜?!)。我到处寻找“暂停菜单”,人们说使用另一层。我试着做了一百万次,但它不起作用(或者我可能是那个不知道的人)=(

到目前为止我做了什么:

**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
}

3 个答案:

答案 0 :(得分:4)

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

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
  }
}

答案 1 :(得分:2)

我认为最好的方法是遵循此stackoverflow answer(换句话说使用SKTAudio class),您可以在其中声明sharedInstance音频库以轻松管理所有音频操作。

通过这门课程,您可以举例说明您的问题:

var audioManager : SKTAudio = SKTAudio.sharedInstance()

self.audioManager.pauseBackgroundMusic()

答案 2 :(得分:0)

我会使用AVAudioPlayer,你可以轻松启动和停止声音。我从未使用过SKAction.playSoundFileNamed,所以我不确定是否可以停止。