1

我有两个节点和一个布尔值。很简单。当节点 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];
}

touchesBegan方法

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

接触结束

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

开始联系

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

1 回答 1

3

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

我相信我曾经遇到过这个问题,解决方案是当你触摸它时创建一个新的身体。这个“应该”触发了 didBeginContact 下一个循环。您也许还可以更改物理身体上的属性,但如果我没记错的话,我没有让它起作用并且不得不初始化一个新的物理身体。

例如,尝试使用此更新您的 touchesBegan

touchesBegan方法

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

希望这对你有用。

于 2015-02-01T17:10:59.773 回答