2014-03-13 52 views
0

我有這種方法,應該循環通過我的節點,並檢查我的父母有多少管,如果它小於6,我應該添加更多。前6項工作,但在他們被刪除行動[self.children count]刪除後仍然報告他們仍然是6管。即使屏幕上的節點數爲0,它也會記錄「Tube Amount:6/6」。我需要一個精確的方法來查看我有多少個節點。這裏是我的代碼:self.children count沒有給出正確的數字

-(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]]]; 

    } 
} 
+0

'children'看起來是一個集合,可能是一個數組。你並沒有從這個'children'數組中刪除任何東西。因此你得到了第6點。你只是在這裏和那裏改變'last'指針。 –

+2

也許'SKAction'不喜歡被同時添加到兩個節點。你有沒有嘗試爲每個管單獨創建一個'[SKAction removeFromParent]'? –

+0

@AnoopVaidya您可能忽略了他爲每個孩子添加一個'[SKAction removeFromParent]'的事實,這個孩子在執行時應該從'self.children'數組中移除孩子。 –

回答

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]]]];