- (void)didBeginContact:(SKPhysicsContact *)拒绝触发

时间:2013-12-30 22:59:29

标签: ios sprite-kit skphysicsbody

1)我在SKScene标题上设置了委托协议:

@interface WorldScene : SKScene <SKPhysicsContactDelegate>

2)我将委托设置为物理世界:

- (id) init
{
    self = [super init];
    if (self)
    {
        self.physicsWorld.gravity = CGVectorMake(0,0);
        self.physicsWorld.contactDelegate = self;
        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    }
    return self;
}

3)我设置了我的位掩码:

static const uint32_t playerCategory  =  0x1 << 0;
static const uint32_t wallsCategory  =  0x1 << 1;
static const uint32_t endCategory  =  0x1 << 2;

4)我制作了我的播放器:

    SKSpriteNode *player = [[SKSpriteNode alloc] initWithColor:[SKColor blueColor] size:CGSizeMake(PLAYERSIZE, PLAYERSIZE)];
player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
player.name = @"player";
player.physicsBody.dynamic = YES;
player.physicsBody.categoryBitMask = playerCategory;
player.physicsBody.collisionBitMask = wallsCategory;
player.physicsBody.contactTestBitMask = endCategory;

5)我制作了我的最终拼贴:

SKSpriteNode *tile = [[SKSpriteNode alloc] initWithColor: [SKColor whiteColor] size:CGSizeMake(TILESIZE,TILESIZE)];

if ([cell isEnd]){
    tile.color = [SKColor greenColor];
    tile.name = @"endTile";
    tile.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: tile.size];
    NSLog(@"END TILE OF SIZE %f x %f", tile.size.width, tile.size.height);
    tile.physicsBody.dynamic = YES;
    tile.physicsBody.affectedByGravity = NO;
    tile.physicsBody.categoryBitMask = endCategory;
    tile.physicsBody.collisionBitMask = 0;
    tile.physicsBody.contactTestBitMask = playerCategory;

}

6)但这永远不会被召唤! :(

- (void)didBeginContact:(SKPhysicsContact *)contact
{

    NSLog(@"Touched!");
    [self removeAllChildren];
}

我正在制作一个迷宫游戏。玩家目前与所有墙壁碰撞都很好,但是我希望游戏在到达最后一个牌时结束。目前,未调用联系事件。我真的不知道什么是错的,我将调试器输出设置为显示所有输出(无论如何,如果它到达事件,所有的孩子都应该被删除)。

Whyyyyyy

1 个答案:

答案 0 :(得分:0)

已解决 - 结果是init从未被调用过,因此委托实际上并未被设置。将所有初始化代码移动到 - (void)didMoveToView:(SKView *)视图,它开始正常工作。