2014-09-18 46 views
0

我建立一個精靈套件「侵略者」的遊戲,大致符合從raywenderlich.comenumerateChildNodesWithName沒有停止

我碰到與enumerateChildNodesWithName方法的問題How To Make a Game Like Space Invaders with Sprite Kit Tutorial: Part 1教程。我使用該方法簡單地沿着它的X軸(從左到右)移動SKSpriteNode,並在節點位於框架右邊緣的100個點內時停止移動。

這裏是我的enumerateChildNodesWithName方法的用法:

-(void)updateMoveInvaders:(NSTimeInterval)currentTime 
{ 
    [self enumerateChildNodesWithName:invaderName usingBlock:^(SKNode *node, BOOL *stop) { 

     [node setPosition:CGPointMake((node.position.x + 1), node.position.y)]; 

     if (CGRectGetMaxX(node.frame) > (self.frame.size.width - 100)){ 
      *stop = YES; 
      NSLog(@"should stop"); 
     } 
    }]; 
} 

的邏輯似乎是正確的,因爲字符串「應該停止」被記錄到控制檯當節點爲100點框架的邊緣之內,但枚舉不會停止並且節點繼續向右移動。爲什麼會發生?

我這樣調用方法,以便:

- (void)update:(NSTimeInterval)currentTime 
{ 
    [self updateMoveInvaders:currentTime]; 
} 

完整的可執行代碼(Xcode項目)可以在GitHub

回答

1

找到這需要updateMoveInvaders每一幀:

- (void)update:(NSTimeInterval)currentTime 
{ 
    [self updateMoveInvaders:currentTime]; 
} 

這意味着這對每個節點都使用invaderName執行每個幀:

[node setPosition:CGPointMake((node.position.x + 1), node.position.y)]; 

如果您每幀繼續執行此行一次,節點是否繼續向右移動看起來並不明顯?

+1

好的。然後巨大的缺乏與更新機制的理解。謝謝 – 2014-09-18 18:31:31