[英]How to Properly Space SKSpriteNodes?
我正在開發一個游戲,其中節點不斷向下滾動。 我將它們設置為在某個位置生成,並且一切看起來都一樣:
當用戶反復暫停游戲時,節點會出現未對齊的問題,如下所示:
我相信,當我暫停游戲時,我的時機就會丟掉。 這是我的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
。 現在,每次游戲暫停或取消暫停時,時間都會更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.