9

SKScene継承クラスを作成しました。問題は、物理ボディメソッドの接触についてです

- (void)didBeginContact:(SKPhysicsContact *)contact 

呼び出されない解決策は簡単かもしれませんが、スプライトキットの初心者として、私はこれにこだわっています。

以下はコードです

#import "MyScene.h"
@interface MyScene ()
@property BOOL contentCreated;
@end
@implementation MyScene
- (id)initWithSize:(CGSize)size {
    self = [super initWithSize:size];
    if (self) {
        self.physicsWorld.contactDelegate = self;
        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    }
    return self;
}
- (void)didMoveToView:(SKView *)view
{
    if (!self.contentCreated) {
        [self buildWorld];
        self.physicsWorld.contactDelegate = self;
    }
}

#pragma mark - World Building
- (void)buildWorld {
    NSLog(@"Building the world");
    SKSpriteNode * sprite1 = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(100,100)];
    sprite1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100,100)];
    sprite1.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) +100);

    SKSpriteNode * sprite2 = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(100,100)];
    sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100,100)];
    sprite2.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - 100);


    [self addChild:sprite1];
    [self addChild:sprite2];
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
    NSLog(@"contact");
}

@end

前もって感謝します。

4

1 に答える 1

13

SKPhysicsWorldドキュメントから:

接触は、2 つの物理体がオーバーラップし、一方の物理体のプロパティがもう一方の物理体のcontactTestBitMaskプロパティと重複する場合に作成されcategoryBitMaskます。

物理体 acategoryBitMaskと aを割り当てる必要がありcontactTestBitMaskます。最初にカテゴリを作成します。

static const uint32_t sprite1Category = 0x1 << 0;
static const uint32_t sprite2Category = 0x1 << 1;

次に、カテゴリとコンタクト テストのビット マスクを割り当てます。

sprite1.physicsBody.categoryBitMask = sprite1Category;
sprite1.physicsBody.contactTestBitMask = sprite2Category;

sprite2.physicsBody.categoryBitMask = sprite2Category;
sprite2.physicsBody.contactTestBitMask = sprite1Category;

SKPhysicsBodyドキュメントからのメモ:

最高のパフォーマンスを得るには、関心のある対話の連絡先マスクにのみビットを設定します。

于 2013-10-30T20:59:34.540 に答える