そのフレーム(子を囲む最小フレーム)にSKNode
触れると、メソッドによって通知されるように、をサブクラス化しようとしています。touchesBegan::
ただし、SKSpriteNode
子供が触れた場合のみ通知されるようです。メソッドをオーバーライドしようとcontainsPoint:
しましたが、成功しませんでした。
これが私のコードです:
@interface gameNode : SKNode {
SKSpriteNode* mainChar;
}
@property (weak, nonatomic) id <gameNodeDelegate> delegate;
@end
@implementation gameNode // When touched, touchesBegan: method not getting called.
-(id) init {
if (self = [super init]) {
self.userInteractionEnabled = YES;
mainChar = [SKSpriteNode node]; // When touched, touchesBegan: method gets called.
mainChar.color = [SKColor redColor];
mainChar.size = CGSizeMake(25, 25);
[mainChar runAction:[SKAction scaleTo:0 duration:0]];
mainChar.position = CGPointMake(100, 100);
[self addChild:mainChar];
SKSpriteNode* n = [SKSpriteNode node]; // When touched, touchesBegan: method gets called.
n.size = mainChar.size;
n.position = CGPointMake(-100, -100);
n.color = [SKColor greenColor];
[self addChild:n];
[mainChar runAction:[SKAction customAnimationWithProperties:CUAPropertiesForScaleBounce(CUADirectionTypeIn, 1)]];
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"That's a touch!");
}
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
@end
mainChar と nSKSpriteNodes
ノードがタッチされ、画面の残りの部分がタッチされた場合にのみ、タッチ イベントが発生します。
それを機能させるために考えることができる唯一の方法はSKSpriteNode
、背景に透明を追加することですが、もっと良い方法があるはずです!
SKNode
子供が子供に触れているかどうかに関係なく、それが受け取る接触を「飲み込む」ようにするにはどうすればよいですか? つまり、SKNodes
のフレームに触れて、メソッドを介して通知したいのですtouchesBegan::
。
更新:SKNode
これはのフレームの問題だと思います。子供を追加した後に行うと..
NSLog(@"Frame Width: %f, Frame Height: %f", self.frame.size.width, self.frame.size.height);
フレームは、高さと幅が 0 として返されます。ただし、実行すると[self calculateAccumulatedFrame]
、正しいサイズが返されます。どうしてこれなの?
更新:この質問は完全に混乱しています。透明を追加するSKSpriteNode
か、SKScene
. 皆様、ご迷惑をおかけして申し訳ありません。