Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to check if an SKSpriteNode has been removed from parent?

I'm trying to check if this SKSpriteNode has been removed from the screen to implement a piece of code if it has but I can't seem to figure it out.

My code;

if (player.position.y > self.frame.size.height) {
    NSLog(@"out of frame");
    [self runAction:[SKAction sequence:@[
                                         [SKAction waitForDuration:0.5],
                                         [SKAction runBlock:^{

        levelMenu *mainMenu = [[levelMenu alloc] initWithSize:self.size];
        [self.view presentScene:mainMenu transition:[SKTransition fadeWithDuration:0.6]];

    }],
                                         ]]];
}

But this isn't working.

Is there anything I could do to test for when it's been removed from the parent. I.e. something along the lines of "If (player hasLeftScene)or(player hasBeenRemovedFromParent)" or something?

I also tried testing for when the player.position.y > 568 (4-inch display) but it didn't work either.

like image 556
Shen Hutah Avatar asked Dec 30 '25 23:12

Shen Hutah


1 Answers

Check for node.parent. It is nil if it has no parent (was removed from one).

if (node.parent) {
// node has parent, was not removed
} else {
// node does not have parent, was removed
}
like image 148
Dvole Avatar answered Jan 01 '26 13:01

Dvole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!