通过子节点枚举为不同类型

时间:2015-11-12 03:16:20

标签: ios swift

我试图枚举我已经将其子类化为VillainSquirrel的所有精灵节点(如下所示),并且我正在尝试访问特定于VillainSquirrel类的属性...但是,我是陷入困境。

这是我得到的错误:

Tuple types '(SKNode, UnsafeMutablePointer<ObjCBool>)' and '()' have a different number of elements (2 vs. 0)

以下是我尝试通过节点枚举为VillainSquirrel的方法:

self.enumerateChildNodesWithName("villainType1") {
        node as! VillainSquirrel, stop in
        if (node.position.y > self.size.height){
            node.brownMarker.position = CGPointMake(node.position.x, self.size.height - 10)
            node.brownMarker.zPosition = 1
            self.addChild(node.brownMarker)
        }
    }

这是我的VillainSquirrel Class(或者至少是它的开头):

class VillainSquirrel: SKSpriteNode {
  var brownMarker = SKSpriteNode()
  var brownMarkerVisible: Bool

  override init(texture: SKTexture!, color: SKColor, size: CGSize) {

    self.brownMarker.zPosition = 1
    self.brownMarker.xScale = 0.25
    self.brownMarker.yScale = 0.25
    self.brownMarker = SKSpriteNode(imageNamed:"brownMarkerTrans.png")
...

非常感谢您提供的任何帮助!谢谢!

1 个答案:

答案 0 :(得分:0)

想出来了!

   self.enumerateChildNodesWithName("villainType1") {
        node, stop in

        let realnode = node as! VillainSquirrel
        if (node.position.y > self.size.height){
            realnode.brownMarker.position = CGPointMake(realnode.position.x, self.size.height - 10)
            realnode.brownMarker.zPosition = 1
            if (!realnode.brownMarkerVisible){
                self.addChild(realnode.brownMarker)
                realnode.brownMarkerVisible = true
            }
        }
        else if (realnode.brownMarkerVisible){
            realnode.brownMarkerVisible = false
            realnode.brownMarker.removeFromParent()

        }
    }

我只需要在块中键入节点作为我的子类spriteNode,我无法通过节点执行,停止在