Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中循环函数?_Ios_Loops_Swift - Fatal编程技术网

Ios 如何在Swift中循环函数?

Ios 如何在Swift中循环函数?,ios,loops,swift,Ios,Loops,Swift,我知道这可能是个愚蠢的问题,但我已经试了好几个小时了,但还是做不到。所以,我在做一个简单的游戏。我有4个功能来杀死我的英雄。他们每个人都在表演不同的动画,所以我想让程序知道到底是谁杀了一个英雄。我的逻辑是将这些函数放入数组中,但Xcode告诉我“调用中缺少参数#1的参数”。我忽略了这一点,试着做一个switch语句。但每个“案例”都有一个错误,告诉我它期待的是一种模式。这是我的密码: func didBeginContact(contact: SKPhysicsContact) { de

我知道这可能是个愚蠢的问题,但我已经试了好几个小时了,但还是做不到。所以,我在做一个简单的游戏。我有4个功能来杀死我的英雄。他们每个人都在表演不同的动画,所以我想让程序知道到底是谁杀了一个英雄。我的逻辑是将这些函数放入数组中,但Xcode告诉我“调用中缺少参数#1的参数”。我忽略了这一点,试着做一个switch语句。但每个“案例”都有一个错误,告诉我它期待的是一种模式。这是我的密码:

func didBeginContact(contact: SKPhysicsContact) {
    dead()
}

var deathArray:Array = [eatenByMouse(), eatenByCat(), eatenByHamster(), eatenByRabbit()]

func dead() {
    switch deathArray {
    case :
        eatenByMouse()
    case:
        eatenByCat()
    case:
        eatenByHamster()
    case:
        eatenByRabbit()
    default:
        cookie
    }

}    
这可能是一个非常简单的问题,但我对这个问题还不熟悉,不知道该怎么办

我认为发布更多代码的相关示例是个好主意。我想,我的问题可能是碰撞。所以我有一个ColliderType:

enum ColliderType:UInt32 {
    case Cookie = 1
    case Rabbit = 2
    case Mouse = 3
    case Hamster = 4
    case Cat = 5
}
然后我为英雄和敌人制作了一个物理身体:

    self.cookie.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(self.cookie.size.width / 2))
self.cookie.physicsBody?.affectedByGravity = false
self.cookie.physicsBody?.categoryBitMask = ColliderType.Cookie.rawValue
self.cookie.physicsBody?.collisionBitMask = ColliderType.Mouse.rawValue
self.cookie.physicsBody?.contactTestBitMask = ColliderType.Mouse.rawValue

self.mouse.physicsBody = SKPhysicsBody(rectangleOfSize: self.mouse.size)
self.mouse.physicsBody?.dynamic = false
self.mouse.physicsBody?.categoryBitMask = ColliderType.Mouse.rawValue
self.mouse.physicsBody?.contactTestBitMask = ColliderType.Cookie.rawValue
self.mouse.physicsBody?.collisionBitMask = ColliderType.Cookie.rawValue    
eatenByMouse()函数的示例:

func eatenByMouse() {
    self.groundSpeed = 0
    self.cookie.hidden = true
 let animateAction = SKAction.animateWithTextures(self.mouseArray, timePerFrame: 0.1)
    self.mouse.runAction(animateAction)
}
我希望它可能会澄清一些事情。
这似乎是一个很容易的问题,我很惭愧我不理解它。

在第5行,您正在定义您的函数数组:

[ eatenByMouse(), eatenByCat(), ... ]
那些带有
()
语法的表达式实际上调用函数并解析为其返回值。尝试只使用函数名,而不调用它们

[ eatenByMouse, eatenByCat, ... ]

在第5行,您正在定义函数数组:

[ eatenByMouse(), eatenByCat(), ... ]
那些带有
()
语法的表达式实际上调用函数并解析为其返回值。尝试只使用函数名,而不调用它们

[ eatenByMouse, eatenByCat, ... ]

在第5行,您正在定义函数数组:

[ eatenByMouse(), eatenByCat(), ... ]
那些带有
()
语法的表达式实际上调用函数并解析为其返回值。尝试只使用函数名,而不调用它们

[ eatenByMouse, eatenByCat, ... ]

在第5行,您正在定义函数数组:

[ eatenByMouse(), eatenByCat(), ... ]
那些带有
()
语法的表达式实际上调用函数并解析为其返回值。尝试只使用函数名,而不调用它们

[ eatenByMouse, eatenByCat, ... ]

我假设您可以在
didBeginContact
中确定死亡类型。因此,您需要在这里确定死亡类型的索引,然后将其传递给
death
函数,以便它可以找到函数引用并执行它

如果死亡类型函数接受参数或返回不同的结果,则需要相应地更改数组的类型

func didBeginContact(contact: SKPhysicsContact) {
    // You will need to determine this index based on the type of contact/death.
    var deathIndex:Int = 0 // Index of the type of death (e.g. eatenByMouse in this case)

    dead(deathIndex)
}

var deathArray:Array<() -> ()> = [eatenByMouse, eatenByCat, eatenByHamster, eatenByRabbit]

func dead(deathIndex: Int) {
   deathArray[deathIndex]()
}   
func-didBeginContact(联系人:skphysiccontact){
//您需要根据接触/死亡的类型确定此索引。
var deathIndex:Int=0//死亡类型指数(如本例中的eatenByMouse)
死亡(死亡指数)
}
var deathArray:Array()>=[eatenByMouse,eatenByCat,eatenByHamster,eatenByRabbit]
func dead(deathIndex:Int){
迪塔雷[迪阿辛德斯]()
}   

我假设您可以在
didBeginContact
中确定死亡类型。因此,您需要在这里确定死亡类型的索引,然后将其传递给
death
函数,以便它可以找到函数引用并执行它

如果死亡类型函数接受参数或返回不同的结果,则需要相应地更改数组的类型

func didBeginContact(contact: SKPhysicsContact) {
    // You will need to determine this index based on the type of contact/death.
    var deathIndex:Int = 0 // Index of the type of death (e.g. eatenByMouse in this case)

    dead(deathIndex)
}

var deathArray:Array<() -> ()> = [eatenByMouse, eatenByCat, eatenByHamster, eatenByRabbit]

func dead(deathIndex: Int) {
   deathArray[deathIndex]()
}   
func-didBeginContact(联系人:skphysiccontact){
//您需要根据接触/死亡的类型确定此索引。
var deathIndex:Int=0//死亡类型指数(如本例中的eatenByMouse)
死亡(死亡指数)
}
var deathArray:Array()>=[eatenByMouse,eatenByCat,eatenByHamster,eatenByRabbit]
func dead(deathIndex:Int){
迪塔雷[迪阿辛德斯]()
}   

我假设您可以在
didBeginContact
中确定死亡类型。因此,您需要在这里确定死亡类型的索引,然后将其传递给
death
函数,以便它可以找到函数引用并执行它

如果死亡类型函数接受参数或返回不同的结果,则需要相应地更改数组的类型

func didBeginContact(contact: SKPhysicsContact) {
    // You will need to determine this index based on the type of contact/death.
    var deathIndex:Int = 0 // Index of the type of death (e.g. eatenByMouse in this case)

    dead(deathIndex)
}

var deathArray:Array<() -> ()> = [eatenByMouse, eatenByCat, eatenByHamster, eatenByRabbit]

func dead(deathIndex: Int) {
   deathArray[deathIndex]()
}   
func-didBeginContact(联系人:skphysiccontact){
//您需要根据接触/死亡的类型确定此索引。
var deathIndex:Int=0//死亡类型指数(如本例中的eatenByMouse)
死亡(死亡指数)
}
var deathArray:Array()>=[eatenByMouse,eatenByCat,eatenByHamster,eatenByRabbit]
func dead(deathIndex:Int){
迪塔雷[迪阿辛德斯]()
}   

我假设您可以在
didBeginContact
中确定死亡类型。因此,您需要在这里确定死亡类型的索引,然后将其传递给
death
函数,以便它可以找到函数引用并执行它

如果死亡类型函数接受参数或返回不同的结果,则需要相应地更改数组的类型

func didBeginContact(contact: SKPhysicsContact) {
    // You will need to determine this index based on the type of contact/death.
    var deathIndex:Int = 0 // Index of the type of death (e.g. eatenByMouse in this case)

    dead(deathIndex)
}

var deathArray:Array<() -> ()> = [eatenByMouse, eatenByCat, eatenByHamster, eatenByRabbit]

func dead(deathIndex: Int) {
   deathArray[deathIndex]()
}   
func-didBeginContact(联系人:skphysiccontact){
//您需要根据接触/死亡的类型确定此索引。
var deathIndex:Int=0//死亡类型指数(如本例中的eatenByMouse)
死亡(死亡指数)
}
var deathArray:Array()>=[eatenByMouse,eatenByCat,eatenByHamster,eatenByRabbit]
func dead(deathIndex:Int){
迪塔雷[迪阿辛德斯]()
}   

哦,谢谢你!它解决了我的第一个问题。但是图案呢?哦,谢谢你!它解决了我的第一个问题。但是图案呢?哦,谢谢你!它解决了我的第一个问题。但是图案呢?哦,谢谢你!它解决了我的第一个问题。但是模式呢?我觉得
switch-deathArray
(实际上整个
switch
语句看起来很滑稽)。看起来您想在
deathArray
中提供一个索引来运行相应的操作,但我看不到任何索引值的引用。我认为这是循环4个函数的最佳方式。我没有这个函数的索引。显然我错了。但我还是不知道怎么做!我没有这个函数的索引。你怎么知道