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

查看:13
本文介绍了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

我还有一个点击和滑动手势,当我点击屏幕上的任意位置时,我可以左右上下移动播放器.

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.

你也应该在暂停游戏的时候改变你的策略,因为暂停 scene.view 使得之后添加 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)
 ...

为您的游戏状态创建一个全局枚举

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
}

最后将实际的暂停代码添加到您的 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天全站免登陆