ios - "Tap To Resume"暂停文本 SpriteKit

标签 ios swift sprite-kit

我知道 SpriteKit 已经在应用程序进入非事件状态时处理暂停游戏,但我想做的是在应用程序重新进入事件状态时添加一个 SKLabelNode“点击恢复”。现在它正在正确调用我的函数并暂停游戏,但没有显示文本。

AppDelegate.swift

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

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

编辑:

下面是我的终端输出的屏幕截图,其中包含我对上述代码的最新编辑: enter image description here

最佳答案

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

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

    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

@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)
    }
    ...
}

关于ios - "Tap To Resume"暂停文本 SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29756777/

相关文章:

ios - 在 iOS 中获取当前位置的静态方法

ios - 从工具栏 IOS 确定文本字段

ios - 在 UITableView 中搜索“意外发现 nil”

ios - Sprite 的圆形路径?

ios - 如何找到特定iTunes App的SKStoreProductParameterCampaignToken和SKStoreProductParameterAffiliateToken?

ios - 将异步 Gif 加载到滚动的 UICollectionView

ios - 打字机效果文字动画

ios - 与 sprite kit 和 swift 颜色匹配

ios - SpriteKit : find all descendants of SKNode of certain class?

ios - UIButton 未触发其目标选择器