我正在开发一个游戏,其中节点不断向下滚动。我将它们设置为在某个位置生成,并且一切看起来都一样:
当用户反复暂停游戏时,节点会出现未对齐的问题,如下所示:
我相信,当我暂停游戏时,我的时机就会丢掉。这是我的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()中调用,非暂停功能在touchesBegan中调用。
请让我知道是否有什么我可以尝试的,或者您需要更多信息。谢谢!
最佳答案
我终于找到了解决方案。我不确定这是否是最佳答案。但是,它有效。我修改了我的函数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
。现在,每次游戏暂停或取消暂停时,时间都会更新。关于ios - 如何正确分配SKSpriteNodes?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40517533/