gpt4 book ai didi

ios - 如何正确分配SKSpriteNode空间?

转载 作者:行者123 更新时间:2023-11-30 12:55:04 26 4
gpt4 key购买 nike

我正在开发一款游戏,其中节点不断向下滚动屏幕。我将它们设置为在某个位置生成,并且一切看起来都很均匀:

Iphone screen with game playing

当用户反复暂停游戏时,就会出现此问题,节点会错位,如下所示:

iphone screen with game playing

我相信当我暂停游戏时,我的时机就会被打乱。这是我的 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 - 如何正确分配SKSpriteNode空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40517533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com