如何使用"enumerateChildNodesWithName";在SpriteKit中使用Swift? [英] How to use "enumerateChildNodesWithName" with Swift in SpriteKit?

查看:191
本文介绍了如何使用"enumerateChildNodesWithName";在SpriteKit中使用Swift?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift在SpriteKit中制作游戏.

I'm using Swift to make a game in SpriteKit.

在Objective-C中,我可以使用以下方法:

In Objective-C I could use the following method:

 (void)enumerateChildNodesWithName:(NSString *)name usingBlock:(void (^)(SKNode *node, BOOL *stop))block

在该*node上执行操作,但是我无法在Swift中使用此功能.基本上,我不知道如何在Swift中引用该节点.

to perform actions on that *node, but I can't get this function working in Swift. Basically, I don't know how to reference that node in Swift.

这是我正在使用的代码,但是"usingBlock:"部分遇到了问题.我已经尝试了许多小时了很多时间,但都没有成功.请帮忙!

This is the code I'm using, but I'm having trouble with the "usingBlock:" part. I've tried many things for many hours, but have not succeeded. Help please!

func spawnEnemy() -> () {
  let enemy = SKSpriteNode(imageNamed: "enemy")
  enemy.name = "enemy"
  enemy.position = CGPointMake(100, 100)
  self.addChild(enemy)
}

func checkCollisions() -> () {
  self.enumerateChildNodesWithName("enemy", usingBlock: ((SKNode!, CMutablePointer<ObjCBool>) -> Void)?)
}

推荐答案

目前,不要相信自动完成功能可以插入所需的代码,它会从标头"中删除签名,但块签名并不相同作为您为块参数插入自己的闭包时所需的声明.

For now, don't trust autocomplete to insert the code you need — it drops in signatures from the "header", but a block signature is not the same as the declaration you need when inserting your own closure for a block parameter.

编写闭包的正式方法是在括号内复制签名,添加局部参数名称,并使用in关键字标记闭包主体的开始:

The formal way to write a closure would be to replicate the signature inside braces, adding local parameter names and using the in keyword to mark the start of the closure body:

self.enumerateChildNodesWithName("enemy", usingBlock: {
    (node: SKNode!, stop: UnsafeMutablePointer <ObjCBool>) -> Void in 
    // do something with node or stop
})

但是Swift的类型推断意味着您不必写那么多.相反,您只能命名参数,因为它们的类型(以及闭包的返回类型)是已知的:

But Swift's type inference means you don't have to write that much. Instead, you can just name the parameters, because their type (as well as the closure's return type) is known:

self.enumerateChildNodesWithName("enemy", usingBlock: {
    node, stop in 
    // do something with node or stop
})

您还可以使用结尾闭包语法:

You can also use trailing closure syntax:

self.enumerateChildNodesWithName("enemy") {
    node, stop in 
    // do something with node or stop
}

(您甚至可以删除本地参数名称,并按位置引用参数,例如$0表示node,但这不是一个好地方,因为它会使代码的可读性大大降低.最好保留$0和朋友用于闭包,其中闭包是显而易见的参数,例如与mapsort一起使用的闭包.)

(You can even drop the local parameter names and refer to parameters by position — e.g. $0 for node — but here isn't a great place to do that because it makes your code far less readable. It's best to reserve $0 and friends for closures where it's blindingly obvious what the parameters are, like the closures you use with map and sort.)

请参见

See Closures in The Swift Programming Language for further explanation.

此外,由于stopUnsafeMutablePointer,因此使用它的语法与ObjC中的有点不同:将stop.memory = true设置为脱离枚举.

Also, because stop is an UnsafeMutablePointer, the syntax for using it is a bit different than in ObjC: set stop.memory = true to break out of enumeration.

这篇关于如何使用"enumerateChildNodesWithName";在SpriteKit中使用Swift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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