I searched trough SO and tried few examples, but I still can't understand this behaviour. On simulator 7.1 tap-through works, but on 8.1 don't work.Also I asked earlier similar question, but not the same as this and I solved it using nodesAtPoint method and then looping trough all nodes and checking node name / class... But this differs because now I use custom Button class which implements touchesBegan and I want it to detect and if possible "swallow" touches.
So I have a simple Button class which is subclass of SKSpriteNode and it has it's own touchesBegan and userInteractionEnabled = YES
. In my view controller property ignoreSiblingsOrder
is set to YES
.
Here is an (simplified) example which can produce described behaviour:
#import "GameScene.h"
@interface Button : SKSpriteNode
-(instancetype)initWithColor:(UIColor *)color size:(CGSize)size;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
@end
@implementation Button
-(instancetype)initWithColor:(UIColor *)color size:(CGSize)size {
self = [super initWithColor:color size:size];
self.userInteractionEnabled = YES;
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%@ hit", self.name);
}
@end
@implementation GameScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.userInteractionEnabled = NO;
SKNode* root = [SKNode new];
root.name = @"root";
SKNode* layer1 = [SKNode new];
SKNode* layer2 = [SKNode new];
layer1.zPosition = -1;//layer1 and layer2 are just containers
layer2.zPosition = -2;
Button* button = [Button spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(100, 100)];
button.name = @"yellow button";
button.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
[layer1 addChild:button];
[root addChild:layer1];
[root addChild:layer2];
[self addChild:root];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Touch detected");
}
@end
I just don't understand why this doesn't work on 8.1...I know that hit-testing goes in opposite order then rendering nodes, but then what would be the right way to achieve tap-through behaviour? So currently what's happening is when I test on 7.1 I got message "yellow button", but on 8.1 I got message "touch detected" (and when I print node name it says root). Also I have been pointed to file a radar because of this, but as I said I solved everything with nodesAtPoint instead of nodeAtPoint so I didn't. And because I thought that that's not a bug, but rather my mistake, because on 7.1 everything was fine. So is this a bug, or something else ?