[ios]暂停游戏被打断的场景

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

另一个问题︰ 我调用函数,生成每次精灵。但当我中断比赛,例如捡呼叫或双击 home 按钮,转到任务管理器,我的场景并不停止和继续调用此函数 (哦,在这个函数中我记得每个随机的时间,因为在 didMoveToView 它调用一次)。所以如何停止时游戏的景象被打断? 谢谢!

func spawnRocks(){

 if gameOver == 0{

    score++
    scoreLabel.text = "\(score)"

    let rockPosition = arc4random() % UInt32(self.frame.height/1.5)
    var rockAmount = CGFloat(rockPosition) - self.frame.size.height/4

    var rockTexture = SKTexture(imageNamed: randomRock())
    rock = SKSpriteNode(texture: rockTexture)

    rock.setScale(randomRockScale())

    rock.position = CGPointMake(CGRectGetMidX(self.frame) + self.frame.size.width, CGRectGetMidY(self.frame) + rockAmount)

    var moveRock = SKAction.moveByX(-self.frame.size.width*2, y: 0, duration: NSTimeInterval(randomRockMove()))
    var removeRock = SKAction.removeFromParent()
    var moveRemoveRock = SKAction.sequence([moveRock, removeRock])
    rock.runAction(moveRemoveRock)
    rockObject.addChild(rock)

    NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(randomRockSpeed()), target: self, selector: Selector("spawnRocks"), userInfo: nil, repeats: false)
 }

}

解决方法 1:

你可以尝试这种想法︰

首先,当您的应用程序将进入背景 applicationDidEnterBackground 将从 AppDelegate 调用,所以你可以设置一个 bool 与 NSUserDefaults,你可以在任何地方访问还有您的项目并将其设置这种方式︰

func applicationDidEnterBackground(application: UIApplication) {
    NSUserDefaults.standardUserDefaults().setBool(true, forKey: "BackGround")

}

现在在你 GameViewController.swift 使如果为 false,当你加载你再像这样的游戏︰

override func viewDidLoad() {
    super.viewDidLoad()

    NSUserDefaults.standardUserDefaults().setBool(false, forKey: "BackGround")
}

现在你可以阅读到, NSUserDefaults 在您的函数,你可以停止它如果你 bool 是真的这种方式︰

func spawnRocks(){

    let backGround = NSUserDefaults().boolForKey("BackGround")
    if !backGround {

        if gameOver == 0{

            score++
            scoreLabel.text = "\(score)"

            let rockPosition = arc4random() % UInt32(self.frame.height/1.5)
            var rockAmount = CGFloat(rockPosition) - self.frame.size.height/4

            var rockTexture = SKTexture(imageNamed: randomRock())
            rock = SKSpriteNode(texture: rockTexture)

            rock.setScale(randomRockScale())

            rock.position = CGPointMake(CGRectGetMidX(self.frame) + self.frame.size.width, CGRectGetMidY(self.frame) + rockAmount)

            var moveRock = SKAction.moveByX(-self.frame.size.width*2, y: 0, duration: NSTimeInterval(randomRockMove()))
            var removeRock = SKAction.removeFromParent()
            var moveRemoveRock = SKAction.sequence([moveRock, removeRock])
            rock.runAction(moveRemoveRock)
            rockObject.addChild(rock)

            NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(randomRockSpeed()), target: self, selector: Selector("spawnRocks"), userInfo: nil, repeats: false)
        }
    }
}

之后,当用户打开应用程序时再从背景可以设置该布尔值 false 再从 AppDelegate 这种方式︰

func applicationWillEnterForeground(application: UIApplication) {
    NSUserDefaults.standardUserDefaults().setBool(false, forKey: "BackGround")
}

希望它将帮助你。

官方微信
官方QQ群
31647020