我有以下代码。当程序运行时,当您单击左上角的暂停按钮时游戏暂停,但中间的未暂停按钮不会显示。似乎程序运行命令暂停游戏,然后运行命令创建取消暂停按钮,但我不知道如何解决这个问题。
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var lanecounter:CGFloat = 0
var lastOpen:CGFloat = 0
var score:Int = 0
var BGspeed:CGFloat = -700
var player:SKSpriteNode?
let scoreLabel = SKLabelNode()
let highScoreLabel = SKLabelNode()
var direction:Int?
let noCategory:UInt32 = 0
let carCategory:UInt32 = 0b1
let playerCategory:UInt32 = 0b1 << 1
let pointCategory:UInt32 = 0b1 << 2
let bumperCategory:UInt32 = 0b1 << 3
var died:Bool?
var pause:Bool = false
var gameStarted:Bool?
var restartBTN = SKSpriteNode()
var pauseBTN = SKSpriteNode()
var unpauseBTN = SKSpriteNode()
func createPauseBTN()
{
pauseBTN = SKSpriteNode(color: SKColor.purple, size: CGSize(width: 100, height: 100))
pauseBTN.position = CGPoint(x: -self.frame.width/2 + 20, y: self.frame.height/2 - 20)
pauseBTN.zPosition = 10
self.addChild(pauseBTN)
}
func createunPauseBTN()
{
unpauseBTN = SKSpriteNode(color: SKColor.purple, size: CGSize(width: 100, height: 100))
unpauseBTN.position = CGPoint(x: 0, y: 0)
unpauseBTN.zPosition = 1000
self.addChild(unpauseBTN)
//pauseGame()
//scene?.view?.isPaused = true
}
func pauseGame() {
scene?.view?.isPaused = true
createunPauseBTN()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches
{
let location = touch.location(in: self)
if died == true
{
if restartBTN.contains(location) {
restartScene()
}
}
if pauseBTN.contains(location) {
print(pause)
createunPauseBTN()
print("asd")
pauseBTN.removeFromParent()
pauseGame()
}
if unpauseBTN.contains(location) {
scene?.view?.isPaused = false
createPauseBTN()
}
}
}
答案 0 :(得分:3)
您的问题是您正在暂停SKView,这会暂停一切。
scene?.view?.isPaused = true
这意味着您的SKScene中的所有内容(如SKActions,addsChildren等)将无法正常工作/停止工作,直到您再次将其设置为false。
创建worldNode并将所有需要暂停的节点添加到该节点通常更好。 Apple也在DemoBots中做到这一点。
所以在你的课堂上添加另一个属性
let worldNode = SKNode()
并将其添加到DidMoveToView
addChild(worldNode)
将您需要暂停的所有其他节点(例如播放器,敌人等)添加到此worldNode
worldNode.addChild(someNode1)
worldNode.addChild(someNode2)
在你的暂停代码中,你应该说这个
worldNode.isPaused = true
physicsWorld.speed = 0
并在简历代码中
worldNode.isPaused = false
physicsWorld.speed = 1
这应该会给你更大的灵活性,因为你仍然可以做某些事情,例如让某些SKActions运行,例如你的背景。它还应该使您的暂停动作更顺畅。
希望这有帮助