如何在父节点在SpriteKit中启用子节点时关闭子节点的用户交互 [英] How to turn off user interaction of child node when parent node has it enabled in SpriteKit

查看:134
本文介绍了如何在父节点在SpriteKit中启用子节点时关闭子节点的用户交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

推荐答案

我通过发送通知来实现这一点父级,其父级,具有水龙头的位置。父父母中的函数会在禁用用户交互并将排放目标设置为父级的情况下生成发射器。也许有些代码是有序的。

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.

在父母:

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

仍然在父母的父母(场景)中,实施spawnParticles:

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

这篇关于如何在父节点在SpriteKit中启用子节点时关闭子节点的用户交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆