我通過發送來自父母的通知,以父母的位置與水龍頭的位置來實現此目的。父 - 母函數產生禁用用戶交互的發射器,並將排放目標設置爲父項。也許一些代碼是有序的。
在父:
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];
}
你是如何檢查觸摸/用戶交互。 – DogCoffee
touchesBegan父母。如果你點擊父母,它就會像按動按鈕一樣產生動畫,併產生一些粒子。 – miek
今天發現了這一點,在節點上設置userInteractionEnabled允許節點本身接收事件。這意味着你會實現touchesBegan:withEvent:在那個實際的對象 – DogCoffee