Search code examples
ios7sprite-kitskemitternode

How to turn off user interaction of child node when parent node has it enabled in SpriteKit


I have an SKNode that has user interaction enabled and I am adding an SKEmitterNode to that and I want the user interaction to be disabled for just the child. This code does not work.

SKNode* parentNode = [[SKNode alloc] init];
parentNode.userInteractionEnabled = YES;
NSString* path = [[NSBundle mainBundle] pathForResource:@"ABCDEFG" ofType:@"xyz"];
SKEmitterNode* childNode = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
childNode.userInteractionEnabled = NO;
[parentNode addChild:childNode];

I also tried setting user interaction to NO after adding to the parent. Is this possible or do I need to add the emitter to the parent's parent somehow?


Solution

  • I achieved this by sending a notification from the parent, to its parent with the location of the tap. The function in the parent-parent spawns the emitter with user interaction disabled and the target of the emissions set to the parent. Perhaps some code is in order.

    In the parent:

    UITouch *touch = [touches anyObject];
    // to get the location in the scene
    CGPoint location = [touch locationInNode:[self parent]];
    NSDictionary* locationDic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: [NSNumber numberWithFloat:location.x],                                                                                          
                                                                                               [NSNumber numberWithFloat:location.y],
                                                                                               nil]
                                                            forKeys:[NSArray arrayWithObjects:@"loc_x", @"loc_y", nil]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Ntf_SpawnParticles"
                                                        object:nil
                                                      userInfo:locationDic];
    

    In the parent's parent (the scene), register for the event:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(spawnParticles:)
                                                 name:@"Ntf_SpawnParticles"
                                               object:nil];
    

    Still in the parent's parent (the scene), implement "spawnParticles":

    - (void)spawnRockDebris:(NSNotification *)ntf
    {
        float x = [[[ntf userInfo] valueForKey:@"loc_x"] floatValue];
        float y = [[[ntf userInfo] valueForKey:@"loc_y"] floatValue];
        CGPoint location = CGPointMake(x, y);
    
        NSString* particlePath = [[NSBundle mainBundle] pathForResource:@"CoolParticles" ofType:@"sks"];
        SKEmitterNode* particleEmitterNode = [NSKeyedUnarchiver unarchiveObjectWithFile:particlePath];
        // set up other particle properties here
        particleEmitterNode.position = location;
        particleEmitterNode.userInteractionEnabled = NO;
        particleEmitterNode.targetNode = [self childNodeWithName:@"particleTargetNode"];
        [self addChild:particleEmitterNode];
    }