我正在开发一个节点不断向下滚动屏幕的游戏。我把它们设置为在某个位置产生,一切看起来均匀:
当用户反复暂停游戏时会出现问题,节点会像这样错位:
我相信当我暂停比赛时,我的时间被抛弃了。这是我的updateCurrentTime()函数。我设置它所以它每0.6秒添加一个新的随机行。
func updateWithTimeSinceLastUpdate (_ timeSinceLastUpdate:CFTimeInterval) {
lastYieldTimeInterval += timeSinceLastUpdate
if lastYieldTimeInterval > 0.6 {
lastYieldTimeInterval = 0
addRandomRow()
}
}
override func update(_ currentTime: TimeInterval) {
if screenIsPaused == 1{
pauseScreen()
}else if screenIsPaused == 0{
unPauseScreen()
screenIsPaused = 2
} else if screenIsPaused == 3{
gameLayer.isPaused = true
}
var timeSinceLastUpdate = currentTime - lastUpdateTimeInterval
lastUpdateTimeInterval = currentTime
if timeSinceLastUpdate > 1 {
timeSinceLastUpdate = 1/60
lastUpdateTimeInterval = currentTime
}
updateWithTimeSinceLastUpdate(timeSinceLastUpdate)
}
以下是我用来暂停游戏的代码:
func pauseScreen(){
overlay.color = UIColor.black
overlay.alpha = 0.6
overlay.size.height = self.size.height
overlay.size.width = self.size.width
overlay.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
overlay.zPosition = 100
addChild(overlay)
gameLayer.isPaused = true
screenIsPaused = 3
label.text = "Press screen to play!"
label.fontColor = SKColor.white
label.fontSize = 50
label.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
addChild(label)
let Fade = SKAction.sequence([SKAction.fadeIn(withDuration: 1), SKAction.fadeOut(withDuration: 1)])
label.run(SKAction.repeatForever(Fade))
}
func unPauseScreen(){
label.removeFromParent()
overlay.removeFromParent()
screenIsPaused = 2
gameLayer.isPaused = false
}
在touchesEnded()中调用pause函数,在touchesBegan中调用un-pause函数。
如果有什么我可以尝试或者您需要更多信息,请告诉我。谢谢!
答案 0 :(得分:0)
我终于找到了解决方案。我不确定这是不是最好的答案;但它有效。我在这里修改了我的函数updateWithTimeSinceLastUpdate
:
func updateWithTimeSinceLastUpdate (_ timeSinceLastUpdate:CFTimeInterval) {
if lastYieldTimeInterval > 0.6 {
lastYieldTimeInterval = 0
addRandomRow()
}
if screenIsPaused == 1{
pauseScreen()
lastYieldTimeInterval += timeSinceLastUpdate
}else if screenIsPaused == 0{
unPauseScreen()
lastYieldTimeInterval += timeSinceLastUpdate
screenIsPaused = 2
} else if screenIsPaused == 3{
gameLayer.isPaused = true
}else if screenIsPaused == 2{
lastYieldTimeInterval += timeSinceLastUpdate
}
}
我移动了screenIsPaused if语句而不是我的update(_ currentTime: TimeInterval)
。另外,我在每个if语句中添加了lastYieldTimeInterval += timeSinceLastUpdate
。现在,每次游戏暂停或取消暂停时,时间都会更新。