【问题标题】:How to turn off user interaction of child node when parent node has it enabled in SpriteKit当父节点在SpriteKit中启用时如何关闭子节点的用户交互
【发布时间】:2013-10-08 01:32:03
【问题描述】:

我有一个启用了用户交互的 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。这是可能的还是我需要以某种方式将发射器添加到父母的父母?

【问题讨论】:

  • 你如何检查触摸/用户交互。
  • touchesBegan 在父节点上。如果您点击父级,它会像按下按钮一样进行动画处理并产生一些粒子。
  • 今天发现了,在节点上设置 userInteractionEnabled 允许节点本身接收事件。这意味着您将在该实际对象上实现 touchesBegan:withEvent:
  • 我是,但是将子节点添加到启用它的节点会逐渐减少。所以,如果你点击孩子,即使它没有 userInteractionEnabled = YES,点击会被发送到链上,直到有东西消耗它。在这种情况下,消费者是父母。

标签: ios7 sprite-kit skemitternode


【解决方案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);
        }
    }
}

【讨论】:

  • 这引起了一些奇怪。它会随机让我点击不是父级的某个地方(可能是因为以前的发射器仍然处于活动状态)。有时它不会让我点击父级,因为正在发射粒子。我希望发射的粒子是空灵的。
  • @miek 我同意,必须有更好的方法。我无法让发射的粒子变得空灵
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    相关资源
    最近更新 更多