Sprite moves two places after being paused and then unpaused

前端 未结 1 1642
别跟我提以往
别跟我提以往 2020-12-12 07:24

I am having trouble figuring out the solution to this and am starting to get very frustrated with it.

I have a pause button and an unpause button in my game scene to

相关标签:
1条回答
  • 2020-12-12 07:34

    As the member above me said the player is not really moving 2 spaces.

    Also you should maybe change your strategy when pausing your game, because pausing the scene.view makes it very hard to add SpriteKit elements afterwards.

    I think a better way is to create a worldNode in your GameScene and add all the sprites that need to be paused to that worldNode. It basically gives you more flexibility pausing a node rather than the whole scene.

    First create a world node property

     let worldNode = SKNode()
    

    and add it to the scene in ViewDidLoad

     addChild(worldNode)
    

    Than add all the sprites you need paused to the worldNode

     worldNode.addChild(sprite1)
     worldNode.addChild(sprite2)
     ...
    

    Create a global enum for your game states

    enum GameState {
        case Playing
        case Paused
        case GameOver
        static var current = GameState.Playing
    }
    

    Than make a pause and resume func in your game scene

    func pause() {
        GameState.current = .Paused
    
        // show pause menu etc
    }
    
    func resume() {
        GameState.current = .Playing
        self.physicsWorld.speed = 1
        worldNode.paused = false
    }
    

    And finally add the actual pause code to your updateMethod. This way it will not resume the game even if spriteKit itself tries to resume (e.g. reopened app, dismissed alert etc)

     override func update(currentTime: CFTimeInterval) {
    
         if GameState.current == .Paused {
              self.physicsWorld.speed = 0
              worldNode.paused = true
         }
    }
    

    In regards to your tapGesture recogniser, in the method that gets called after a tap you can add this before the rest of the code

     guard GameState.current != .Paused else { return }
     ....
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题