此时,一旦我的物体经过一个节点,高分就会不断计数。 我只希望它过去一次算上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];
}
}];
}
我只想用一个数字来计算,我怎么能改变这个?它目前将继续计算其传输的每个像素。
答案 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
如果是调用方法。