在应用启用后让游戏暂停吗? [英] Keeping the game paused after app become active?

查看:149
本文介绍了在应用启用后让游戏暂停吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在该论坛上的第一篇帖子,如果我做的事情不正确,我谨此致歉! :)

It is my first post on this forum and I apologize in advance if I am doing something not in the right way ! :)

我正在用Swift& amp;制作一款iPhone游戏. SpriteKit和我目前正面临一个问题.当我的应用程序进入后台运行时,它会调用功能暂停(请参见下文),但是在游戏恢复时会自动取消暂停.

I am making an iphone game with Swift & SpriteKit and I am currently facing a problem. When my app is going to background it calls a function pause (cf. below) but it automatically unpause when the game resumes.

我看到了一个非常有趣的帖子: Spritekit-在以下情况下保持游戏暂停didBecomeActive (和如何保持SpriteKit场景应用程序启动时暂停?),但我被卡住了.

I have seen this very interesting post : Spritekit - Keep the game paused when didBecomeActive (and How to keep SpriteKit scene paused when app becomes active?) but I am stuck.

我不知道如何实现新的SKView类,因为View的配置如下代码所示.

I don't know how to implement the new SKView class as my View is configured as shown in the below code...

这是我的应用程序的工作方式:

This is how my application works :

class GameViewController: UIViewController {

var scene: GameScene!

override func viewDidLoad() {
    super.viewDidLoad()

    // Configure the View
    let SkView = view as! SKView
    SkView.multipleTouchEnabled = true

    // Create and configure the scene
    scene = GameScene(size: SkView.bounds.size)
    scene.scaleMode = .AspectFill

    // Present the scene
    SkView.presentScene(scene)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("PauseWhenBackGround:"), name:"PauseWhenBackGround", object: nil)
}

func PauseWhenBackGround(notification : NSNotification) {
    if scene.Pausing == false{
        scene.Pause()
    }
}

我尝试了以下方法:

我添加了一个新类:

class GameSceneView : SKView {      
    func CBApplicationDidBecomeActive() {
    }
}

然后,我尝试将视图设置为let SkView = view as! GameSceneView,但是出现错误,提示我无法将视图强制转换为MyProjectName.GameSceneView()... 我还尝试了以下方法:let SkView! = GameSceneView() as GameSceneView!,但最终出现了灰色背景...

Then, I tried to set my view as let SkView = view as! GameSceneView but I got an error saying that I cannot cast the view to MyProjectName.GameSceneView()... I also tried the following : let SkView! = GameSceneView() as GameSceneView! but I end up with a gray background scene...

有人知道我如何实现新的SKView类以防止CBApplicationDidBecomeActive()错误的发生,从而使游戏在激活时不会暂停吗?

Does anyone knows how I can implement the new SKView class to prevent the CBApplicationDidBecomeActive() bug from happening so that the game does not unpause when becoming active ?

非常感谢您! :)

推荐答案

我认为更好的方法是,您可以在GameScene中创建一个worldNode并将所有需要暂停的精灵添加到该worldNode中,而不是暂停整个场景.这样比较好,因为如果您暂停场景,则无法添加暂停菜单节点或使用触摸开始等.基本上,您可以更灵活地暂停节点,而不是整个场景.

I think a better way is instead of pausing the whole scene you could create a worldNode in your GameScene and add all the sprites that need to be paused to that worldNode. Its better because if you pause the scene you cannot add pause menu nodes or use touches began etc. It basically gives you more flexibility pausing a node rather than the whole scene.

首先创建世界节点(如果需要,请创建全局属性)

First create the world node (make global property if needed)

 let worldNode = SKNode()
 addChild(worldNode)

将您需要暂停的所有精灵添加到worldNode

Than add all the sprites you need paused to the worldNode

 worldNode.addChild(sprite1)
 worldNode.addChild(sprite2)

为您的不同游戏状态创建一个枚举

Create an enum for your different game states

enum GameState {
    case Playing
    case Paused
    case GameOver
    static var current = GameState.Playing
}

比在游戏场景中发挥暂停作用

Than make a pause func in your game scene

 func pause() {
     GameState.current = .Paused
     //self.physicsWorld.speed = 0 // in update
     //worldNode.paused = true     // in update

     // show pause menu etc
 }

并像上面使用NSNotification那样调用它,甚至可以使用委托更好地调用它.

And call it like you did above using NSNotification or even better using delegation.

我更喜欢这种方法,而不是从gameViewController中暂停场景,也暂停整个场景.

I prefer this method way more than pausing the scene from the gameViewController and also pausing the whole scene.

创建简历方法

 func resume() {
        GameState.current = .Playing
        self.physicsWorld.speed = 1
        worldNode.paused = false  

        // remove pause menu etc
 }

最后将其添加到您的更新方法中

and finally add this to your update method

override func update(currentTime: CFTimeInterval) {

  if GameState.current == .Paused { 
      self.physicsWorld.speed = 0
      worldNode.paused = true
}

当应用再次激活或取消了诸如购买应用内购买之类的警报时,Spritekit有时倾向于恢复游戏.为避免这种情况,我总是使用update方法将代码实际暂停游戏.

Spritekit sometimes tends to resume the game when the app becomes active again or when an alert such as for in app purchases is dismissed. To avoid this I always put the code to actually pause the game in the update method.

希望这会有所帮助.

这篇关于在应用启用后让游戏暂停吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆