2016-11-09 2 views
0

Я работаю над игрой, где узлы непрерывно прокручиваются по экрану. Я их настроить на нерест в определенном положении, и все выглядит еще:Как правильно разместить SKSpriteNodes?

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 ,

Пожалуйста, дайте мне знать, если есть что-нибудь, что я могу попробовать, или если вам требуется дополнительная информация. Благодаря!

ответ

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 если заявления здесь вместо моего update(_ currentTime: TimeInterval). Кроме того, я добавил lastYieldTimeInterval += timeSinceLastUpdate в каждое из операторов if. Теперь каждый раз, когда игра приостанавливается или приостанавливается, время обновляется.