self.children count没有给出正确的数字

时间:2014-03-13 06:12:59

标签: ios objective-c sprite-kit

我有这个方法应该遍历我的节点并检查我父母中有多少管,如果它少于6,我应该添加更多。前6项工作,但是在通过移除动作[self.children count]移除它们之后仍然报告它们仍然是6管。它记录"管数量:6/6"即使在屏幕上节点计数为0.我需要一个准确的方法来查看我有多少个节点。这是我的代码:

-(void) checkTubes {
    int amt = 0;
    SKSpriteNode *last;
    for(SKSpriteNode *node in self.children)
        if(node.size.height > 100) {
            last = node;
            amt++;
        }
    NSLog(@"Tube Amount: %i/%i", amt, [self.children count]);
    if(amt < 6) {
        for(SKSpriteNode *node in self.children)
            if(node.size.height > 100)
                last = node;
        SKSpriteNode *tube1 = [SKSpriteNode spriteNodeWithImageNamed:@"tube"];
        SKSpriteNode *tube2 = [SKSpriteNode spriteNodeWithImageNamed:@"tube2"];
        int x1 = 155;
        int y1 = -100;
        int x2 = x1;
        int y2 = y1+tube1.size.height + 68;
        if(last != (id)[NSNull null]) {
            x1 = last.position.x + x1;
            x2 = x1;
        }
        tube1.position = CGPointMake(x1, y1);
        tube2.position = CGPointMake(x2, y2);
        [self addChild:tube1];
        [self addChild:tube2];

        SKAction *actionMove1 = [SKAction moveTo: CGPointMake(tube1.position.x - 1600, tube1.position.y) duration: 15.55f];
        SKAction *actionMove2 = [SKAction moveTo: CGPointMake(tube2.position.x - 1600, tube2.position.y) duration: 15.55f];
        SKAction *actionMoveDone = [SKAction removeFromParent];
        [tube1 runAction:[SKAction sequence:@[actionMove1, actionMoveDone]]];
        [tube2 runAction:[SKAction sequence:@[actionMove2, actionMoveDone]]];

    }
}

1 个答案:

答案 0 :(得分:0)

如果不复制操作,则不能对多个节点使用相同的操作。这可能会解决它:

    SKAction *actionMoveDone = [SKAction removeFromParent];
    [tube1 runAction:[SKAction sequence:@[actionMove1, actionMoveDone]]];

    // 2nd and any following use of the same action must be a 'copy'
    [tube2 runAction:[SKAction sequence:@[actionMove2, [actionMoveDone copy]]]];