Sprite moves two places after being paused and then unpaused

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 allow the player to pause the game, which is the following code

  • Maintain a Multipeer Connectivity session in Background via BackgroundTask?
  • iOS application integration with pinterest
  • How can I create a big, red UIButton with the iPhone SDK?
  • Different Bundle Identifier for different build configurations
  • how to change the scale of view
  • Copy file to root of XCode project when building
  •  else if (node == pauseButton) {
    
            pauseButton.removeFromParent()
            addChild(unpauseButton)
            addChild(restartButton)
            self.runAction (SKAction.runBlock(self.pauseGame))
    
        }
    
    
    
    func pauseGame(){
    
        pauseButton.hidden = true
        unpauseButton.hidden = false
    
        scene!.view!.paused = true // to pause the game
    
    }
    

    Problem

    The problem is that when I pause the game then unpause the game my player sprite seems to move two spaces forward automatically

    I also have a tap and swipe gesture that allows me to move the player up left right and down when I tap anywhere on the screen.

    func tapUp(){
    
        let amountToMove:CGFloat = levelUnitHeight
    
        let move:SKAction = SKAction.moveByX(0, y: amountToMove, duration: 0.1)
    
    
        menubutton.hidden = true
        settingsButton.hidden = true
        highscoreLabel.hidden = true
        pauseButton.hidden = false
    
        thePlayer.runAction(move)
    
    }
    
    func swipedRight(){
    
        let amountToMove:CGFloat = levelUnitHeight
    
        let move:SKAction = SKAction.moveByX(amountToMove, y: 0, duration: 0.1)
    
        thePlayer.runAction(move) // links the action with the players
    
    }
    

    Solutions Collect From Internet About “Sprite moves two places after being paused and then unpaused”

    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