我有一个启用了用户交互的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。这是可能的还是我需要以某种方式将发射器添加到父母的父级?
答案 0 :(得分:1)
我确信有更好的方式(希望有!!),但这是一个开始。
也许这是应该怎么做的。问题是如果你有一个sprite上的发射器,触摸不会通过(在我的测试中没有)。
-(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);
}
}
}
答案 1 :(得分:0)
我通过从父母发送通知向其父级发送通知来实现此目的。父母中的函数生成发射器,禁用用户交互并将发射的目标设置为父级。也许有些代码是有序的。
在父母:
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];
}