0
votes

I hava an NSMutableArray with my CCNodes in it. I want to paint and unpaint them on my CCScene, using [scene addChild:] and [CCNode removeFromParentAndCleanUp:YES]. My problem shows up when it's checking if any of my objects has a parent and it is deleted in another class at the same time. You can see this code, and it always crashes there:

for(int y = negativeY; y < positiveY; y++){
    for(int x = negativeX; x < positiveX; x++){
        if([[self.rockArray objectAtIndex:y] objectAtIndex:x] != nil){ //checking if object is not null
            if([[[self.rockArray objectAtIndex:y] objectAtIndex:x] parent] == nil){ //Crashes here!
                NSLog(@"Rock is not visible. Painting it");
                [listener addChild:[[self.rockArray objectAtIndex:y] objectAtIndex:x]];
            }else{
                NSLog(@"Rock is already visible.");
            }
        }
    }
}

The Log message is *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull parent]: unrecognized selector sent to instance 0x351d6b0'

Maybe we could solve this using Semaphores? (if so, could you please post me an example in Objective C?)

Thank you for your time!

1
Figure out why you have NSNull instances in your array. That would be the root cause of this.YvesLeBorg
FYI ... as user of the cocos2d API, your code is running everything on the main thread, so in effect there is no such thing as 'at the same time' ... semaphores would be a solution looking for a problem to solve. Adding threads in a cocos2d app is completely unadvisable because of open GL.YvesLeBorg
Yes, the array does have NSNull pointers, as I said. The problem was I was checking if it was nil instead of [NSNull null]. Thank you.aramusss

1 Answers

3
votes

Check your object like this:

[[self.rockArray objectAtIndex:y] objectAtIndex:x] != [NSNull null]

before sending it parent message.

The crash happens because by some reason an object in your array is not CCNode but NSNull which does not respond to parent selector.

You should not check if it's nil because collections can not keep nil pointers