如何在SKSpriteNode上的Swift中使用完成处理程序进行连续的函数调用? [英] How do I do consecutive function calls using the completion handler in Swift on an SKSpriteNode?

查看:77
本文介绍了如何在SKSpriteNode上的Swift中使用完成处理程序进行连续的函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用三个函数,每个函数完成特定数量的循环后,一个接一个.

I am trying to get three functions to call, one after the other after each has completed a specific number of loops.

一旦具有计数的第一个函数完成,我就成功尝试调用第二个函数.

I have succeeded in trying to call a second function once a first function with a count has completed.

我正在努力进行第三,第四等操作.我似乎遇到的问题是视图中调用的语法确实加载了:

I am struggling to do a third, fourth, etc. The problem I appear to have is in the syntax for the call in the view did load:

midGroundlevelMovingSlow(midGroundlevelMovingMedium(midGroundlevelMovingFast()))

midGroundlevelMovingSlow(midGroundlevelMovingMedium(midGroundlevelMovingFast()))

不适用于以下三个功能:

Does not work for the three functions where as:

midGroundlevelMovingSlow(midGroundlevelMovingMedium)

midGroundlevelMovingSlow(midGroundlevelMovingMedium)

工作了两个.

我尝试过改变括号,添加自我.等等,都没有成功.

I have tried altering the brackets, adding self. etc with no success.

我用于这三个功能(慢速,中速和快速)的代码是:

The code that I have used for the three functions (slow, medium and fast) is:

func midGroundlevelMovingSlow(completion: (()->Void)?) {

    let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed)
    let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
    let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 2)

    var callCount = 2

    mgImage.runAction(movingAndReplacingLevelImage){ () -> Void in
        if --callCount == 0 {
            self.foreGroundLevelMovingMedium(self.foreGroundLevelMovingFast)
        }
    }

    mgImage2.runAction(movingAndReplacingLevelImage){ () -> Void in
        if --callCount == 0 {
            self.foreGroundLevelMovingMedium(self.foreGroundLevelMovingFast)
        }
    }
}

其次:

func midGroundlevelMovingMedium(completion: (()->Void)?) {

    let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed / 2)
    let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
    let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 4)

    var callCount = 2

    mgImage.runAction(movingAndReplacingLevelImage){ () -> Void in
        if --callCount == 0 {
            self.midGroundlevelMovingFast()
        }
    }

    mgImage2.runAction(movingAndReplacingLevelImage){ () -> Void in
        if --callCount == 0 {
            self.midGroundlevelMovingFast()
        }
    }
}

最后:

func midGroundlevelMovingFast() {

    let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed / 4)
    let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
    let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 8)

    mgImage.runAction(movingAndReplacingLevelImage)
    mgImage2.runAction(movingAndReplacingLevelImage)
}

当我使用与最后两个相同的格式时,我认为代码是好的.在我用来在viewdidload中调用函数的那一行中的某个地方是错误的,或者无法像这样完成.

When I use the last two in the same format as I used before it works so I think the code is good. Somewhere in the line i'm using to call the function in the viewdidload is either wrong or cannot be done like this.

有人可以向我解释为什么这不起作用吗?

Can anyone explain to me why this is not working please?

谢谢

史蒂芬

更新:

好的,再次感谢您的帮助,我觉得我现在已经很近了.但是,我仍然遇到最后一个错误,我不知道该如何解决.

Ok, thanks again for your help and I feel that I'm so close now. However I'm still getting a final error that I don't know how to resolve.

我现在有:

    //Functions for the flow of the level, controlling the speed of the background images and the number of loops each will go through before increasing in speed.

    func midGroundLevelMovingSlow(completion: (()->Void)?) {

        let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed)
        let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
        let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 4)

        var callCount = 2

        mgImage.runAction(movingAndReplacingLevelImage){ () -> Void in
            if --callCount == 0 {
                completion?()
            }
        }

        mgImage2.runAction(movingAndReplacingLevelImage){ () -> Void in
            if --callCount == 0 {
                completion?()
            }
        }
    }

其次:

    func midGroundLevelMovingMedium(completion: (()->Void)?) {

        let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed / 2)
        let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
        let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 4)

        var callCount = 2

        mgImage.runAction(movingAndReplacingLevelImage){ () -> Void in
            if --callCount == 0 {
                completion?()
            }
        }

        mgImage2.runAction(movingAndReplacingLevelImage){ () -> Void in
            if --callCount == 0 {
                completion?()
            }
        }
    }

然后:

    func midGroundLevelMovingFast(completion: (()->Void)?) {

        let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed / 4)
        let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
        let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 4)

        var callCount = 2

        mgImage.runAction(movingAndReplacingLevelImage){ () -> Void in
            if --callCount == 0 {
                completion?()
            }
        }

        mgImage2.runAction(movingAndReplacingLevelImage){ () -> Void in
            if --callCount == 0 {
                completion?()
            }
        }
    }

与谁联系:

    midGroundLevelMovingSlow(completion: {
        midGroundLevelMovingMedium(completion: {
            midGroundLevelMovingFast(completion: {
                print "Finished"
            })
        })
    })

在调用中的外部参数标签完成""的每一行上给我一个错误

Which gives me an error on each line "extraneous argument label "completion" in call"

有人知道为什么会这样吗?

Does anyone know why this is the case?

谢谢

史蒂芬

更新:

这只是删除多余的完成"一词的情况.

It was just a case of deleting the extraneous "completion" word.

现在工作,很棒.

推荐答案

在runAction的完成处理程序中,您应该在方法中调用完成处理程序

In the completion handler in runAction you should be calling the completion handler in your method

mgImage.runAction(movingAndReplacingLevelImage){ () -> Void in
        if --callCount == 0 {
            completion()
        }
    }

但是,调用这种方法变得越来越丑陋,因此,我认为研究如何使用SKAction.sequence和SKAction.group解决此问题是一个好主意.

However calling the methods like this growing increasingly ugly so I think it's a good idea to look into how to solve this with SKAction.sequence and SKAction.group.

https ://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKAction_Ref/#//apple_ref/occ/clm/SKAction/序列:

https ://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKAction_Ref/#//apple_ref/occ/clm/SKAction/group :

我与SpriteKit的合作并不多,所以我无法通过炉膛回复您如何做,但我确定有一种解决方案,可以解决您内置在SpriteKit中的问题.

I haven't worked much with SpriteKit so I can't reply tell you by hearth how to do it but I'm sure there is a solution for what you are trying to do built in to SpriteKit.

func midGroundlevelMovingSlow(completion: (()->Void)?) {

    let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed)
    let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
    let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 2)

    var callCount = 2

    mgImage.runAction(movingAndReplacingLevelImage){ () -> Void in
        if --callCount == 0 {
            completion()
        }
    }

    mgImage2.runAction(movingAndReplacingLevelImage){ () -> Void in
        if --callCount == 0 {
            completion()
        }
    }
}

通话中

midGroundlevelMovingSlow(completion: {
    self.foreGroundLevelMovingMedium(completion: {
        self.foreGroundLevelMovingFast(completion:{})
    })
})

这篇关于如何在SKSpriteNode上的Swift中使用完成处理程序进行连续的函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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