如何在节点上的Position.x位于另一个节点上的position.x之后停止计数

时间:2014-12-15 19:46:15

标签: ios if-statement sprite-kit void

此时,一旦我的物体经过一个节点,高分就会不断计数。 我只希望它过去一次算上1。

-(void)increment
{
    self.number ++;
    self.text = [NSString stringWithFormat:@"%i", self.number];
    NSLog (@"%i", self.number);
} 


-(void)handlePoints
{
    [world enumerateChildNodesWithName:@"obstacle" usingBlock:^(SKNode *node, BOOL *stop) {
        if (node.position.x < hero.position.x) {
            PMPointsLabel *pointsLabel = (PMPointsLabel *)[self childNodeWithName:@"pointsLabel"];
            [pointsLabel increment];
        }
    }];

}

我只想用一个数字来计算,我怎么能改变这个?它目前将继续计算其传输的每个像素。

1 个答案:

答案 0 :(得分:0)

您的问题有点令人困惑,因为如果您的目标只是在enumerateChildNodesWithName上添加一次,或者仅在完全通过它后再增加一点就不清楚。

做前者

-(void)handlePoints
{
    [world enumerateChildNodesWithName:@"obstacle" usingBlock:^(SKNode *node, BOOL *stop) {
        if (node.position.x < hero.position.x) {
            PMPointsLabel *pointsLabel = (PMPointsLabel *)[self childNodeWithName:@"pointsLabel"];
            [pointsLabel increment];

            *stop = YES; // To be extra safe you can always test for stop being non-nil, but it should always be non-nil
        }
    }];
}

设置*stop = YES将停止枚举。

如果你想停止它&#34;永远&#34;或者直到某些其他条件,你需要在课堂上存储额外的状态以防止它。您需要基于handlePoints内部或handlePoints调用方法中的条件的逻辑,以防止枚举(如果它在handlePoints或调用handlePoints如果是调用方法。