Swift 中的 Sprite Kit SKAction 的麻烦



我设置的SKAction序列有问题。

此序列的目标是获取一个包含 9 个精灵节点的数组,并一次显示一个。 然后,此序列将在当前节点的右侧或左侧显示节点,具体取决于按下的按钮(右按钮或左按钮)。

我在这里遇到了一些有趣的事情。 它似乎像现在一样工作,但是如果我快速向左或向右多次按下按钮,它似乎跟不上并且该过程失败。 我没有收到错误,精灵节点只是不显示或显示不正确。

我在下面附上了我的代码。 因为当我缓慢循环时它正在工作,所以我相信这是一个时间问题,也许我需要添加一个完成块。 我尝试这样做,但没有成功。

我的问题是,是否有任何明显的突出之处在这里看起来不对劲,您认为完成块可能会解决问题吗?

func pressedButtonRight(){
    if currentDisplayedWorld < 8 {
        var currentWorld:SKSpriteNode = worldArray[currentDisplayedWorld] as SKSpriteNode
        var nextWorld:SKSpriteNode = worldArray[currentDisplayedWorld + 1] as SKSpriteNode
        nextWorld.position = CGPointMake(self.frame.size.width, 50)
        self.addChild(nextWorld)
        let move = SKAction.moveByX(-self.frame.size.width, y: 0,
            duration: 1.0, delay: 0,
            usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5)
        //currentWorld.runAction(move)
        nextWorld.runAction(move)
        currentWorld.removeFromParent()
        currentDisplayedWorld++
    }else if currentDisplayedWorld == 8 {
    }
}
func pressedButtonLeft(){
    if currentDisplayedWorld > 0 {
        var currentWorld:SKSpriteNode = worldArray[currentDisplayedWorld] as SKSpriteNode
        var previousWorld:SKSpriteNode = worldArray[currentDisplayedWorld - 1] as SKSpriteNode
        previousWorld.position = CGPointMake(-self.frame.size.width, 50)
        self.addChild(previousWorld)
        let moveBack = SKAction.moveByX(self.frame.size.width, y: 0,
            duration: 1.0, delay: 0,
            usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5)
        //currentWorld.runAction(moveBack)
        previousWorld.runAction(moveBack)
        currentWorld.removeFromParent()
        currentDisplayedWorld--
    }else if currentDisplayedWorld == 0 {
    }
}

完美。 我在下面发布了我的最终代码供所有人查看。 再次感谢您的帮助。

    func pressedButtonRight(){
    if currentDisplayedWorld < 8 {
        if self.moving == false {
            self.moving = true
            var currentWorld:SKSpriteNode = self.worldArray[currentDisplayedWorld] as SKSpriteNode
            var nextWorld:SKSpriteNode = self.worldArray[currentDisplayedWorld + 1] as SKSpriteNode
            nextWorld.position = CGPointMake(self.frame.size.width, 50)
            self.addChild(nextWorld)
            let move = SKAction.moveByX(-self.frame.size.width, y: 0, duration: 1.0, delay: 0,usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5)
            //currentWorld.runAction(move)
            nextWorld.runAction(move, completion: {
                self.moving = false
            })
            currentWorld.removeFromParent()
            currentDisplayedWorld++
        }
    }else if currentDisplayedWorld == 8 {
    }
}
func pressedButtonLeft(){
    if currentDisplayedWorld > 0 {
        if self.moving == false {
            self.moving = true
            var currentWorld:SKSpriteNode = self.worldArray[currentDisplayedWorld] as SKSpriteNode
            var previousWorld:SKSpriteNode = self.worldArray[currentDisplayedWorld - 1] as SKSpriteNode
            previousWorld.position = CGPointMake(-self.frame.size.width, 50)
            self.addChild(previousWorld)
            let moveBack = SKAction.moveByX(self.frame.size.width, y: 0, duration: 1.0, delay: 0,
                usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5)
            //currentWorld.runAction(moveBack)
            previousWorld.runAction(moveBack, completion: {
                    self.moving = false
            })
            currentWorld.removeFromParent()
            currentDisplayedWorld--
        }
    }else if currentDisplayedWorld == 0 {
    }
}

最新更新