Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

ios - 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

 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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...