jmat
jmat

Reputation: 320

enumerateChildNodesWithName not stopping

I'm building a Sprite Kit "Invader" game that loosely follows the How To Make a Game Like Space Invaders with Sprite Kit Tutorial: Part 1 tutorial from raywenderlich.com

I've run into a problem with the enumerateChildNodesWithName method. I'm using the method to simply move a SKSpriteNode along it's X axis (from left to right) and stop the movement when the node is within 100 points of the frame's right edge.

Here's my usage of the enumerateChildNodesWithName method:

-(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");
        }
    }];
}

The logic appears to be correct, as the string "should stop" gets logged to the console when the node is within 100 points of the frame's edge, but the enumeration doesn't stop and the node keeps moving to the right. Why would this happen?

I call this method like so:

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

The full executable code (Xcode project) can be found on GitHub

Upvotes: 0

Views: 209

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

This calls updateMoveInvaders every frame:

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

Which means this is executed every frame for every node with invaderName:

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

Doesn't it seem obvious that the node keeps moving to the right if you keep executing this line once every frame?

Upvotes: 1

Related Questions