0

そのフレーム(子を囲む最小フレーム)に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. 皆様、ご迷惑をおかけして申し訳ありません。

4

1 に答える 1