SpriteKit:在场景暂停时运行动作 [英] SpriteKit: run action while scene is paused

查看:98
本文介绍了SpriteKit:在场景暂停时运行动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮可以在我的代码上暂停游戏。我想要的是将游戏与该按钮暂停会发出一条消息,显示已暂停。但是,由于场景暂停,因此不会显示该消息。

I have a button to pause the game on my code. What I want is that pausing the game with that button makes a message that says "Paused" to appear. However, since the scene is paused, the message does not appear.

我现在所拥有的是一个SKLabelNode,其开头的alpha值为0.0,当用户暂停游戏时,它会使用fadeInWithDuration()更改为1.0。然后,当用户再次按下按钮时,它会使用fadeOutWithDuration()更改回0.0。问题是当场景暂停时,带有fadeInWithDuration()的SKAction不会运行。

What I have right now is a SKLabelNode with the alpha on 0.0 at the beginning and when the user pauses the game, it changes to 1.0 with fadeInWithDuration(). Then when the user presses the button again, it changes back to 0.0 with fadeOutWithDuration(). The problem is that the SKAction with fadeInWithDuration() does not run when the scene is paused.

我怎么能实现这个?

推荐答案

苹果公司在DemoBots中使用的最佳方法是创建一个暂停而不是场景的世界节点。

The best way, one Apple also uses in "DemoBots", is to create a world node that you pause instead of the scene.

创建一个worldNode属性

Create a worldNode property

class GameScene: SKScene {

    let worldNode = SKNode()
}

将其添加到didMoveToView中的场景

add it to the scene in didMoveToView

addChild(worldNode)

然后将您需要的所有内容添加到worldNode。这包括通常由场景运行的动作(例如,计时器,敌人产卵等)

and than add everything you need paused to the worldNode. This includes actions that are normally run by the scene (eg. timers, enemy spawning etc)

worldNode.addChild(someNode)
worldNode.run(someSKAction)

比暂停功能要好

worldNode.isPaused = true
physicsWorld.speed = 0

和简历

worldNode.isPaused = false
physicsWorld.speed = 1

如果您有更新的功能,您还可以在更新功能中添加额外的支票想要在暂停时忽略。

You can also add an extra check in your Update function if you have stuff there that you want to ignore when paused.

override func update(_ currentTime: CFTimeInterval) {

    guard !worldNode.isPaused else { return }

    // your code
}

这样,当游戏暂停时,添加暂停的标签或其他用户界面会更容易,因为您实际上并未暂停场景。除非将该操作添加到worldNode或worldNode的子节点,否则您还可以运行所需的任何操作。

This way it's much easier to add your paused label or other UI when your game is paused because you haven't actually paused the scene. You can also run any action you want, unless that action is added to the worldNode or to a child of worldNode.

希望这有助于

这篇关于SpriteKit:在场景暂停时运行动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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