didBeginContact 方法未按预期工作

didBeginContact method not working as intended

我有两个节点和一个布尔值。很简单。当节点 A 联系节点 B 并且布尔值为 0 时,没有任何反应。但是,如果布尔值为 1,则通过 didBeganContact 方法删除节点 A。

非常简单,但是当我想要删除节点 A 时,我遇到了一个恼人的问题。

节点 B 是一个矩形,节点 A 是一个位于矩形中间的正方形,当我使用 touchesBegan 方法点击并按住节点 B 时,布尔值被调用并变为 1。现在,在节点 A 接触节点 B 之前,我点击并按住节点 B,当节点 A 接触时,它被移除,但是当节点 A 已经在中间时,我点击节点 B,没有任何反应,我不知道为什么。

矩形法

-(void)rectangle
{
SKSpriteNode *rectangle = [[SKSpriteNode alloc] init];
rectangle = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(75, 150)];
rectangle.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
rectangle.name = @"rect";
rectangle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rectangle.size];
rectangle.physicsBody.categoryBitMask = rectangleCategory;
rectangle.physicsBody.contactTestBitMask = fallingSquareCategory;
rectangle.physicsBody.collisionBitMask = 0;

[self addChild:rectangle];
}

touchesBeganMethod

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

if ([node.name isEqualToString:@"rect"])
{
    radBool = 1;
}
}

touchesEnded

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

if ([node.name isEqualToString:@"rect"])
{
    radBool = 0;
}
}

平方法

-(void)square
{
SKAction *move = [SKAction moveToY:CGRectGetMidY(self.frame) duration:1.75];

SKSpriteNode *fallingSquare = [[SKSpriteNode alloc] init];
fallingSquare = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(75, 75)];
fallingSquare.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame));
fallingSquare.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:fallingSquare.size];
fallingSquare.physicsBody.categoryBitMask = fallingSquareCategory;
fallingSquare.physicsBody.contactTestBitMask = rectangleCategory
fallingSquare.physicsBody.collisionBitMask = 0;

[self addChild:fallingSquare];
[fallingSquare runAction:move];
}

didBeginContact

static inline SKSpriteNode *nodeFromBody(SKPhysicsBody *body1, SKPhysicsBody *body2, uint32_t category) {
SKSpriteNode *node = nil;
if (body1.categoryBitMask & category) {
    node = (SKSpriteNode *)body1.node;
}
else if (body2.categoryBitMask & category) {
    node = (SKSpriteNode *)body2.node;
}
return node;
}

-(void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;

SKSpriteNode *R1 = nil;
SKSpriteNode *fallingS = nil;

firstBody = contact.bodyA;
secondBody = contact.bodyB;

R1 = nodeFromBody(firstBody, secondBody, rectangleCategory);
fallingS = nodeFromBody(firstBody, secondBody, fallingSquareCategory);

if (R1 && fallingS && radBool == 1)
{
    [fallingS removeFromParent];
}
}

我认为您的问题是 didBeginContact 的 "begin" 部分。它只会在他们第一次联系时被调用,而不是每个循环。因为在他们第一次联系时 bool 没有设置为 YES,所以它永远不会被再次评估。

我相信我 运行 曾经遇到过这个问题,解决方案是在您触摸它时创建一个新的物理 body。这 "should" 触发了 didBeginContact 的下一步。您也许还可以更改物理 body 上的 属性,但如果我没记错的话,我没有让它工作,不得不初始化一个新的物理 body。

例如尝试用这个更新你的 touchesBegan

touchesBeganMethod

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if ([node.name isEqualToString:@"rect"])
    {
        radBool = 1;
        node.physicsBody = nil;
        node.physicsBody = [SKPhysicsBody     bodyWithRectangleOfSize:rectangle.size];
        node.physicsBody.categoryBitMask = rectangleCategory;
        node.physicsBody.contactTestBitMask = fallingSquareCategory;
        node.physicsBody.collisionBitMask = 0;
    }
}

希望对你有用。