循环遍历SKNode的所有孩子?

编程入门 行业动态 更新时间:2024-10-19 07:27:04
本文介绍了循环遍历SKNode的所有孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有多个子SKSpriteNodes的SKNode。一个简单的例子:

I have an SKNode with a number of child SKSpriteNodes. A simplified example:

var parentNode = SKNode() var childNode1 = SKSpriteNode() var childNode2 = SKSpriteNode() self.addChild(parentNode) parentNode.addChild(childNode1) parentNode.addChild(childNode2)

我想对所有这些孩子运行 colorizeWithColor 操作。当我在 parentNode 上运行操作时,没有任何效果。

I want to run a colorizeWithColor action on all of these children. When I run the action on parentNode, there's no effect.

我不能使用 enumerateChildNodesWithName 在父项上,因为它的许多子项已经有我正在使用的名称。

I can't use enumerateChildNodesWithName on the parent, because many of its children already have names I'm using.

有没有办法循环所有子项 parentNode ,为了对所有这些操作执行单个操作?

Is there a way of looping through all children of parentNode, in order to run a single action on all of them?

推荐答案

您可以简单地枚举 parentNode.children :

for child in parentNode.children as! [SKNode] { // ... }

如有必要,检查每个孩子是否实际上是 SKSpriteNode :

If necessary, check each child if it is actually a SKSpriteNode:

for child in parentNode.children { if let spriteNode = child as? SKSpriteNode { // ... } }

从 Swift 2(Xcode 7)开始,枚举和可选的强制转换可以与带有案例模式的for循环结合使用:

As of Swift 2 (Xcode 7), enumeration and optional cast can be combined with to a for-loop with a case-pattern:

for case let child as SKSpriteNode in parentNode.children { // ... }

更多推荐

循环遍历SKNode的所有孩子?

本文发布于:2023-11-25 22:54:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   孩子   SKNode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!