Ios 在Spritekit中创建暂停菜单
我试图在游戏层上创建一个暂停菜单。我只是在上面加了两个按钮,当暂停按钮被按下时,玩家仍然可以看到游戏的舞台Ios 在Spritekit中创建暂停菜单,ios,swift,sprite-kit,swift4,Ios,Swift,Sprite Kit,Swift4,我试图在游戏层上创建一个暂停菜单。我只是在上面加了两个按钮,当暂停按钮被按下时,玩家仍然可以看到游戏的舞台 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches { let location = touch.location(in: self) if atPoint(location).name == "
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if atPoint(location).name == "pause" {
let button: SKSpriteNode?
let button2: SKSpriteNode?
button = SKSpriteNode(imageNamed: "button_continue")
button?.name = "button_continue"
button?.size.height = 55
button?.size.width = 215
button?.physicsBody = SKPhysicsBody(rectangleOf: button!.size)
button?.physicsBody?.allowsRotation = false
button?.position = CGPoint(x: 0, y: 160)
button?.zPosition = 100
self.addChild(button!)
button2 = SKSpriteNode(imageNamed: "button_finish")
button2?.name = "button_finish"
button2?.size.height = 55
button2?.size.width = 215
button2?.physicsBody = SKPhysicsBody(rectangleOf: button2!.size)
button2?.physicsBody?.allowsRotation = false
button2?.position = CGPoint(x: 0, y: -50)
button2?.zPosition = 100
self.addChild(button2!)
// The two buttons are created but here I am trying to stop everything until the continue button is pressed and I don't find a way
}
}
}
这就是我尝试过的,但不起作用
然后,当按下“继续”按钮时,我也会删除按钮,但我可以正确编码。“完成”按钮也会将玩家发送到主菜单场景。我要做的是将控件(按钮)放置在名为
controlsLayer
的层中,该层与所有游戏项目(如玩家、敌人和障碍物)分开。然后我把所有我想暂停的游戏项目放在一个名为gameLayer
的层中。我使controlsLayer
有一个高zPosition
像100
然后,当我单击暂停按钮时,我会调用
gameLayer.isPaused=true
,当我单击继续时,我会调用相反的按钮gameLayer.isPaused=false
。只需暂停游戏层,您仍然可以在暂停按钮上运行其他操作,如过渡或效果。如果暂停整个场景,您将无法对任何项目运行任何操作或效果。视图将停止,但某些计时器出现问题,无法停止。我认为这是因为目标是“自我”,我把它改成了游戏玩家,这甚至是最糟糕的。那我怎么才能阻止它们呢?你的代码中没有显示任何计时器!。这就是为什么Spritekit IMO中不应使用计时器的原因。在Spritekit问题中,有几十条评论/帖子讨论了在Spritekit中使用计时器的缺点。我是swift的新手,不知道,已经更改了它,现在工作得很好。Ps:我必须添加addChild(controlLayer)
,然后从父项中移除。如果我的评论帮助你回答了你的问题,你应该将其标记为正确,你真的应该将你的标题改为类似“在Spritekit中暂停计时器”的内容。你的标题具有误导性。在Spritekit中暂停游戏和创建暂停菜单通常使用.isPaused属性处理。
while atPoint(location).name != "button_cotinue" {
timer1.invalidate()
}