Sprite在暂停后取消暂停后会移动两个位置 [英] Sprite moves two places after being paused and then unpaused

查看:107
本文介绍了Sprite在暂停后取消暂停后会移动两个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到解决方案,并开始对此感到非常沮丧。

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

}

问题

问题是,当我暂停游戏然后取消暂停游戏时,我的玩家精灵似乎移动了两个空格自动转发

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

我还有一个点击和滑动手势,可以让我移动pl当我点击屏幕上的任意位置时,ayer向左上下左右。

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

}


推荐答案

正如我上面的成员所说,玩家并没有真正移动2个空格。

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

此外,你应该在暂停游戏时改变你的策略,因为暂停场景。视图非常困难之后添加SpriteKit元素。

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.

我认为更好的方法是在GameScene中创建一个worldNode,并将需要暂停的所有精灵添加到该worldNode。它基本上可以让您更灵活地暂停节点而不是整个场景。

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()

并将其添加到ViewDidLoad中的场景

and add it to the scene in ViewDidLoad

 addChild(worldNode)

将所有需要暂停的精灵添加到worldNode

Than add all the sprites you need paused to the worldNode

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

创建全局enum for your game states

Create a global enum for your game states

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

比暂停和在你的游戏场景中恢复func

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
}

最后将实际的暂停代码添加到updateMethod。这样即使spriteKit本身试图恢复也不会恢复游戏(例如重新打开应用程序,解除警报等)

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

关于tapGesture识别器,在点按后调用的方法中可以在其余代码之前添加这个

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 }
 ....

希望这会有所帮助

这篇关于Sprite在暂停后取消暂停后会移动两个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆