SO を検索していくつかの例を試しましたが、まだこの動作を理解できません。シミュレーター7.1ではタップスルーが機能しますが、8.1では機能しません。また、以前に同様の質問をしましたが、これと同じではなく、nodesAtPointメソッドを使用して解決し、すべてのノードをループしてノード名/クラスを確認しました..しかし、今は touchesBegan を実装するカスタム Button クラスを使用しており、タッチを検出し、可能であれば「飲み込む」ようにしたいため、これは異なります。
したがって、SKSpriteNode のサブクラスである単純な Button クラスがあり、独自の 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では「タッチが検出されました」というメッセージが表示されました(ノード名を出力すると、ルートと表示されます)。また、このためにレーダーを提出するように指摘されましたが、私が言ったように、nodeAtPointではなくnodesAtPointですべてを解決したので、そうしませんでした。それはバグではなく、私のミスだと思ったからです。なぜなら、7.1 ではすべて問題なかったからです。これはバグですか、それとも何か他のものですか?