Swift中的Sprite Kit SKAction问题

时间:2014-12-04 19:09:56

标签: swift sprite-kit

我遇到了我设置的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 {

    }
}

1 个答案:

答案 0 :(得分: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 {

    }
}