ios – “点击恢复”暂停文本SpriteKit

前端之家收集整理的这篇文章主要介绍了ios – “点击恢复”暂停文本SpriteKit前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道当应用程序进入非活动状态时,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
            }
        }
    }
    ...
}

编辑:

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

解决方法

所以我在这里“破解”了我的解决方案.感谢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已经处理了中断,我真正需要做的就是暂停动作和物理.

GameScene.swift

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

    override func didMoveToView(view: SKView) {
        ...
        NSNotificationCenter.defaultCenter().addObserver(self,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,and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene",object: self)
    }
    ...
}

猜你在找的iOS相关文章