2

我有一个启用了用户交互的 SKNode,我正在向其中添加一个 SKEmitterNode,我希望仅为孩子禁用用户交互。此代码不起作用。

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];

添加到父级后,我还尝试将用户交互设置为 NO。这是可能的还是我需要以某种方式将发射器添加到父母的父母?

4

2 回答 2

1

我确信有更好的方法(希望有!!),但它是一个开始。

也许这就是它应该这样做的方式。问题是如果你在精灵上有一个发射器,触摸不会通过(在我的测试中没有)。

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {

        UITouch *touch = [touches anyObject];
        CGPoint positionInScene = [touch locationInNode:self];
        SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];

        if (touchedNode.userInteractionEnabled) {
            NSLog(@"Name of node touched %@", touchedNode.name);
        }
        else {
            NSLog(@"Can't touch this! %@", touchedNode.name);
        }
    }
}
于 2013-10-08T03:11:18.907 回答
0

我通过从父级向其父级发送带有水龙头位置的通知来实现这一点。parent-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];

在父级的父级(场景)中,注册事件:

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

仍然在父级的父级(场景)中,实现“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];
}
于 2013-10-10T01:42:20.087 回答