didBeginContact方法无法正常工作

时间:2015-02-01 07:06:37

标签: ios objective-c sprite-kit skphysicscontact

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

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

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

矩形方法

-(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;
}
}

square方法

-(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];
}
}

1 个答案:

答案 0 :(得分:3)

我相信你的问题是didBeginContact的“开始”部分。它只会在第一次接触时调用,而不是每个循环。因为bool在第一次接触时没有设置为YES,所以永远不会再次评估。

我相信我曾经遇到过这个问题,解决方法是在触摸它时创建一个新的物理身体。这个“应该”触发didBeginContact下一个转发。你也许还可以改变身体上的一个属性,但是如果我没记错的话,我没有那个工作,不得不初始化身体。

例如,尝试使用此

更新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;
    }
}

希望对你有用。