“点击恢复”暂停文本SpriteKit [英] "Tap To Resume" Pause text SpriteKit

查看:72
本文介绍了“点击恢复”暂停文本SpriteKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道当应用程序进入非活动状态时,SpriteKit已处理暂停游戏但我正在尝试做的是在应用程序重新进入活动状态时添加SKLabelNode点击以恢复。现在它正在调用我的函数并暂停游戏,但文本没有显示。

I know SpriteKit already handles pausing the game when the app enters the inactive state but what I'm trying to do is add a SKLabelNode "tap to resume" when the app re-enters the active state. Right now it's calling my functions correctly and pausing the game, but the text is not showing.

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    println("applicationWillResignActive")
    NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self)
    NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseText", object: self)
    ...
}



GameScene.swift



GameScene.swift

class GameScene: SKScene, SKPhysicsContactDelegate {
    ...
    let tapToResume = SKLabelNode(fontNamed: "Noteworthy")
    ...
    override func didMoveToView(view: SKView) {
        ...
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGameScene"), name: "PauseGameScene", object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("showPauseText"), name: "ShowPauseText", object: nil)

        tapToResume.text = "tap to resume"
        tapToResume.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
        tapToResume.fontSize = 55
        tapToResume.hidden = true
        self.addChild(tapToResume)
        ...
    }

    func pauseGameScene() {
        println("pause game")
        self.view?.paused = true
    }

    func showPauseText() {
        if self.view?.paused == true {
            tapToResume.hidden = false
            println("show text")
        }
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        ...
        if self.paused {
            self.view?.paused = false
            if tapToResume.hidden == false {
                tapToResume.hidden = true
            }
        }
    }
    ...
}

编辑:

下面是我的终端输出的屏幕截图,其中包含我对上述代码的最新修改:

Below is a screenshot of my terminal output with my latest edits to my above code:

推荐答案

所以我在这里破解了我的解决方案。感谢 ABakerSmith ,并建议设置 self.speed = 0.0 ,操作暂停,我的标签会出现但 physicsWorld 仍然有效。所以我的解决方案是设置 self.speed = 0.0 AND self.physicsWorld.speed = 0.0 。当应用程序从非活动状态返回时,我只重置 self.speed = 1.0 self.physicsWorld.speed = 1.0 。我确信还有其他解决方案可以应对这种困境但是由于SpriteKit已经处理了中断,我真正需要做的就是暂停动作和物理。

So I "hacked" my solution here. Thanks to ABakerSmith with the suggestion of setting self.speed = 0.0, the actions were paused and the my label will appear but the physicsWorld was still active. So my solution was to set self.speed = 0.0 AND self.physicsWorld.speed = 0.0. When the app returns from the inactive state, I just reset self.speed = 1.0 and self.physicsWorld.speed = 1.0. I'm sure there are other solutions to this dilemma but since SpriteKit already handles interruptions, all I really needed to do was pause the actions and the physics.

class GameScene: SKScene, SKPhysicsContactDelegate {
    let tapToResume = SKLabelNode(fontNamed: "Noteworthy")
    ...

    override func didMoveToView(view: SKView) {
        ...
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGameScene"), name: "PauseGameScene", object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("showPauseText"), name: "ShowPauseText", object: nil)
    }

    func pauseGameScene() {
        self.physicsWorld.speed = 0.0
        self.speed = 0.0
    }

    func showPauseText() {
        if self.physicsWorld.speed == 0.0 {
        tapToResume.hidden = false
        }
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        ...
        if self.physicsWorld.speed == 0.0 {
            self.physicsWorld.speed = 1.0
            self.speed = 1.0
            if tapToResume.hidden == false {
                tapToResume.hidden = true
            }
        }
    }

    ...
}



AppDelegate.swift



AppDelegate.swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self)
    NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseText", object: self)
    }
    ...
}

这篇关于“点击恢复”暂停文本SpriteKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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