SpriteKit碰撞阻止推出

时间:2014-11-09 11:27:09

标签: objective-c sprite-kit collision-detection skphysicsbody

我有这个问题: 我有节点A和节点B. 我想要的是当它们之间发生碰撞时(节点A与B的顶部发生碰撞),节点B不会推动节点A向上一点。因为就像现在一样,当A与B碰撞时,它被推回一点,并且碰撞立即结束(调用didEndContact)。因此当我喜欢与B碰撞时,碰撞立即结束,然后重新开始,然后结束,然后重新开始......我想要的是,如果A与B相撞,A将会放在此B.我注意到我可以实现通过设置A.dynamic = NO,但我需要将速度应用于A,如果它不是动态的,速度就不会起作用。

这是代码:

typedef NS_OPTIONS(uint32_t, CollisionCategory)
{
    CollisionCategoryPlayer             =  1 << 0,
    CollisionCategoryDiggable           =  1 << 1,
};

-(void)didMoveToView:(SKView *)view
{
    self.playerNode = [SKSpriteNode spriteNodeWithImageNamed:@"player"];
    self.playerNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.playerNode.size];
    self.playerNode.physicsBody.categoryBitMask = CollisionCategoryPlayer;
    self.playerNode.physicsBody.collisionBitMask = CollisionCategoryDiggable;
    self.playerNode.physicsBody.contactTestBitMask = CollisionCategoryDiggable;
    self.playerNode.position = CGPointMake(160, 520);
    self.playerNode.name = @"player";
    self.playerNode.physicsBody.allowsRotation = NO;
    self.playerNode.physicsBody.friction = 0;
    [self addChild:self.playerNode];

    SKSpriteNode* spriteNode = [SKSpriteNode spriteNodeWithTexture:textToUse];
    spriteNode.name = keyFromCoordinate(coord);
    spriteNode.position = CGPointMake(160, 400);
    spriteNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.size];
    spriteNode.physicsBody.dynamic = NO;
    spriteNode.physicsBody.categoryBitMask = CollisionCategoryDiggable;
    spriteNode.physicsBody.collisionBitMask = CollisionCategoryPlayer;
    spriteNode.physicsBody.contactTestBitMask = CollisionCategoryPlayer;
    spriteNode.physicsBody.resting = YES;
    [self addChild:spriteNode];

}

-(void)update:(NSTimeInterval)currentTime
{
    self.playerNode.physicsBody.velocity = CGVectorMake(0, -200);
}

问题再次出现了didBeginContact被调用,然后是didEndContact,然后是didBegin等等,我想要的是 - 开始时只调用一次。

1 个答案:

答案 0 :(得分:0)

为这两个physicsBodies设置restitution = 0帮助