I would like some of my Sprite Kit nodes to behave like UIButtons. I tried 2 approaches:
1) Use touchesBegan:
- this works if a user is careful, but seems to fire multiple times, faster than I can disable interaction, resulting in a button being able to be activated multiple times:
spriteNode.userInteractionEnabled = YES;
//causes the following to fire when node receives touch
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.userInteractionEnabled = NO;
DLog(@"Do node action");
self.userInteractionEnabled = YES;
}
2)I switched to Kobold-Kit as a layer on top of iOS Sprite Kit. One of the things it allows me to do is add button - like behavior to any node. However, I'm running into an issue where a if I have 2 buttons stacked on top of each other, tapping the top button activates both. Boolean flags can prevent repeated interactions in this case. I tracked the issue of stacked buttons firing together to this call within KKScene:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
for (id observer in _inputObservers)
{
if ([observer respondsToSelector:@selector(touchesBegan:withEvent:)])
{
[observer touchesBegan:touches withEvent:event];
}
}
}
The scene simply sends notifications to all nodes within the scene. This causes buttons behaviors that are stacked on top of each other to fire together.
Is there a way or example that shows how to properly arrange sprite nodes to allow behavior similar to UIButton, where I can have only the top button activate, and each activation disables the button for a short time afterwards?