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