【发布时间】:2015-06-09 01:40:18
【问题描述】:
我通过 SO 搜索并尝试了几个示例,但我仍然无法理解这种行为。在模拟器 7.1 上分接工作,但在 8.1 上不起作用。我之前也问过类似的问题,但与此不同,我使用 nodesAtPoint 方法解决了它,然后循环遍历所有节点并检查节点名称/类.. . 但这有所不同,因为现在我使用实现 touchesBegan 的自定义 Button 类,我希望它能够检测并在可能的情况下“吞下”触摸。
所以我有一个简单的 Button 类,它是 SKSpriteNode 的子类,它有自己的 touchesBegan 和 userInteractionEnabled = YES。在我看来,控制器属性ignoreSiblingsOrder 设置为YES。
这是一个可以产生描述行为的(简化的)示例:
#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
我只是不明白为什么这在 8.1 上不起作用...我知道命中测试的顺序与渲染节点的顺序相反,但是实现点击行为的正确方法是什么?所以目前发生的事情是,当我在 7.1 上测试时,我收到消息“黄色按钮”,但在 8.1 上,我收到消息“检测到触摸”(当我打印节点名称时,它显示为 root)。我也因此而成为pointed to file a radar,但正如我所说,我用nodeAtPoint而不是nodeAtPoint解决了所有问题,所以我没有。因为我认为这不是错误,而是我的错误,因为在 7.1 上一切都很好。那么这是一个错误,还是别的什么?
【问题讨论】:
标签: ios objective-c sprite-kit simulator zposition