1
votes

Is there a method that gets called every time a node is deleted? or added?

Along the terms of touchesBegan a method that gets called every time a touch on the screen happens.

or touchesMoved a method that gets called when a node gets touched and moved.

So is there such a method that gets called when a node gets removeFromParent?

I am currently using the update method to detect if a node is still present like:

SKNode *n = [self childNodeWithName: @"n"];
if (!n)
{
    // no n
    if (life != 0)
    {
        [self addChild:_n);
        life--;
    }
}

Now when ever that node is missing I remove 1 life, and then add the node again.

The problem is the update method is running too fast that 2 lives are removed every time the node is removed. I believe this is because adding the node back is kinda slower that the update method. So it takes 2 loops on the update method before it detects that the node is present again.

2
how do you remove your node? you should call life--; in the method where you remove your nodeJulio Montoya
I also remove it by detecting if it is out of the screen in the update method. If it is, then I do removeFromParent on it. I have tried that, and the same result happened.majidarif
what happens after you remove the node? a new node appear right?Julio Montoya
@JechtMontoya that is right. But it runs through update twice before the node even gets added again. It seems like the adding of the node takes time. and the update method is faster.majidarif
i post you my answer, sorry for the delay i'm at workJulio Montoya

2 Answers

3
votes

There is no such method. But you can create one.

Create a subclass of (for example) SKSpriteNode and override all "remove" methods (or just those you are using). Within that method send a message to whichever object needs to receive the removal event, or send a notification and register other objects to receive that notification. Don't forget to call the super implementation of the overridden method.

2
votes

if you run this lines inside update

SKNode *n = [self childNodeWithName: @"n"];
if (!n)

that statement will run forever until you stop the game or your node is not equal to nil

to correct that behaviour you need to create a BOOL value

BOOL _shouldProceed;

change the name if you want

when you create your node set BOOL value to true or yes

- (void)createNode {
    _shouldProceed = YES;

    // Your implementation for the node
}

and put this inside update

SKNode *n = [self childNodeWithName:@"n"];

if (!n && _shouldProceed) {
    _shouldProceed = NO;

    if (life != 0) {
        [self createNode];
         life--;
    }
}

if your node is equal to nil and BOOL value is equal to true/yes you need to set BOOL value to false/NO

with this approach this statement only will happens once

when you create a new node with

[self createNode];

the BOOL value is set to true/YES

and that should solve your problems

Good Luck!!