当我的游戏在精灵套件中暂停时,如何返回主场景

时间:2016-02-05 13:49:29

标签: ios swift sprite-kit

当我暂停比赛时,我有一个停顿场景。

我放了3个按钮:

  • RateApp按钮

  • BackToMain按钮。

  • ResumeGame按钮。

会发生什么:

  • RateApp按钮正常工作。

  • ResumeGame按钮正常工作。

无效:

  • BackToMain按钮。

为什么?

因为我的游戏仍在暂停。

PauseGame功能

func pauseGame() {
        isPause = true
        self.view?.paused = true
        pointsLabel.hidden = true
        BooCharacter.hidden = true
        PauseButton.hidden = true
        if #available(iOS 9, *) {
        RecordButton.hidden = true
        }

        PauseScreen = SKSpriteNode()
        PauseScreen.name = "PauseScreen"
        PauseScreen.position = CGPointMake(self.size.width/2, self.size.height/2)
        PauseScreen.color = UIColor.blackColor()
        PauseScreen.alpha = 0.9
        PauseScreen.size = CGSizeMake(1242,2208)
        addChild(PauseScreen)


        PauseLable.text = "Pause"
        PauseLable.fontSize = 75
        PauseLable.fontName = "Futura-Medium"
        PauseLable.fontColor =  UIColor(red:0.98, green:0.82, blue:0.32, alpha:1.0)
        PauseLable.position = CGPoint(x: self.size.width/2, y: self.size.height/2 + 170)
        addChild(PauseLable)


        ContinueButton.name = "CountinueButton"
        ContinueButton.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        ContinueButton.size = CGSizeMake(100, 100)
        addChild(ContinueButton)

        HomeButton.position = CGPoint(x: self.size.width/2 - 50, y: self.size.height/2 - 200)
        HomeButton.size = CGSizeMake(70 , 70)
        HomeButton.name = "HomeButton"
        HomeButton.runAction(SKAction.moveToY(90, duration: 0.6))
        HomeButton.removeFromParent()
        addChild(HomeButton)


        RateButton.position = CGPoint(x: self.size.width/2 + 50, y: self.size.height/2 - 200)
        RateButton.size = CGSizeMake(70 , 70)
        RateButton.name = "RateButton"
        RateButton.runAction(SKAction.moveToY(90, duration: 0.6))
        RateButton.removeFromParent()
        addChild(RateButton)


    }

已更新:

    let scene = GameScene(fileNamed:"GameScene")
    var HomeButton : UIButton!
    var RateButton : UIButton!



    override func viewDidLoad() {
        super.viewDidLoad()

        if (scene != nil) {
            // Configure the view.
            let skView = self.view as! SKView
            //skView.showsFPS = true
            //skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true
            //skView.showsPhysics = true

            NSNotificationCenter.defaultCenter().addObserver(self, selector: "goBack", name: "goBackClick", object: nil);

            /* Set the scale mode to scale to fit the window */
            scene!.scaleMode = .AspectFill

            skView.presentScene(scene)
        }
}

        override func viewWillLayoutSubviews() {
            super.viewWillLayoutSubviews()

                HomeButton.frame = CGRectMake(self.view!.frame.size.width/2 - 50, self.view!.frame.size.height/2 - 200, 70, 70)
                HomeButton.setImage(UIImage(named: "HomeButtonPause.png"), forState: UIControlState.Normal)
                HomeButton.addTarget(self, action: "pressedHomeButton", forControlEvents: .TouchUpInside)

                RateButton.frame = CGRectMake(self.view!.frame.size.width/2 + 50, self.view!.frame.size.height/2 - 200, 70, 70)
                RateButton.setImage(UIImage(named: "RateButtonPause.png"), forState: UIControlState.Normal)
              RateButton.addTarget(self, action: "pressedRateButton", forControlEvents: .TouchUpInside)

            self.view.addSubview(HomeButton)
            self.view.addSubview(RateButton)
        }

  func pressedHomeButton(sender: UIButton!) {
       //Back to main screen
     SKTAudio.sharedInstance().playSoundEffect("Click Button Sound.mp3"); 
          NSNotificationCenter.defaultCenter().removeObserver(self, name: "goBackClick", object: nil)                             
    self.dismissViewControllerAnimated(true, completion: {
    });

    }

      func pressedRateButton(sender: UIButton!) {
            //Rate App        
            SKTAudio.sharedInstance().playSoundEffect("Click Button Sound.mp3");
            UIApplication.sharedApplication().openURL(NSURL(string: "https://itunes.apple.com/us/app/boo-adventure/id1030047247?ls=1&mt=8")!)  //add your link here
            print("Rate App!")

        }

我尝试为我的2个按钮创建2个子视图(主页按钮,速率按钮) 我为他们创建了UIButtons但Xcode返回错误!

PauseGame功能已更新!

   func pauseGame() {
        isPause = true
        GameViewController().ContinueGame()
        pointsLabel.hidden = true
        BooCharacter.hidden = true
        PauseButton.hidden = true
        if #available(iOS 9, *) {
        RecordButton.hidden = true
        }

        PauseScreen = SKSpriteNode()
        PauseScreen.name = "PauseScreen"
        PauseScreen.position = CGPointMake(self.size.width/2, self.size.height/2)
        PauseScreen.color = UIColor.blackColor()
        PauseScreen.alpha = 0.9
        PauseScreen.size = CGSizeMake(1242,2208)
        addChild(PauseScreen)


        PauseLable.text = "Pause"
        PauseLable.fontSize = 75
        PauseLable.fontName = "Futura-Medium"
        PauseLable.fontColor =  UIColor(red:0.98, green:0.82, blue:0.32, alpha:1.0)
        PauseLable.position = CGPoint(x: self.size.width/2, y: self.size.height/2 + 170)
        addChild(PauseLable)


        ContinueButton.name = "CountinueButton"
        ContinueButton.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        ContinueButton.size = CGSizeMake(100, 100)
        addChild(ContinueButton)

        GameViewController().viewWillLayoutSubviews()

    }

1 个答案:

答案 0 :(得分:0)

尝试这样可以带您回到GameScene的开头

func goToGameScene(){
    let gameScene:GameScene = GameScene(size: self.view!.bounds.size) // create your new scene
    let transition = SKTransition.fadeWithDuration(1.0) // create type of transition (you can check in documentation for more transtions)
    gameScene.scaleMode = SKSceneScaleMode.Fill
    self.view!.presentScene(gameScene, transition: transition)
    }

如果这不起作用,请告诉我。