0

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

 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

}
Astrum
  • 375
  • 6
  • 21
  • The only thing I can say, is you THINK that the player is moving 2 spaces, but he really isn't. What is happening is you pause the game, the player has moved, but is not drawn on the screen to move. Once the game is unpaused, then it the actions continue, the drawing takes place, and he is moved. What you can do, is make a boolean to pause the game and set that in the touch event, then the very first thing you do in your update, is check this pause state, and if paused, do `scene!.view!.paused = true` – Knight0fDragon Jan 15 '16 at 14:17

1 Answers1

2

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

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • Pausing the view doesn't disable touch events. – 0x141E Jan 15 '16 at 17:24
  • ok, so that didn't help it still moves two forward, how would i go about setting up the boolean so it disables the touches when the game is paused and only allow for the resume and restart button to be pressed? because I also have a tap gesture that moves the player. Should i do it for both or just the tap gesture? – Astrum Jan 16 '16 at 04:46
  • Create the bool "paused"and than in your methods that get called after the tap gesture you would say something like this "guard !paused else { return } before the rest of the code. – crashoverride777 Jan 19 '16 at 11:36
  • that cured one of the two moves that the player makes after being resumed but it still makes it move one space forward I asked a different question for that and his answer was to remove the gesture and re add it after resuming and it worked do you think that is a good answer? here is the question http://stackoverflow.com/questions/34868333/cancelling-a-tap-gesture-recongiser – Astrum Jan 19 '16 at 23:32