How do I correctly use allContactedBodies?

前端 未结 3 763
梦毁少年i
梦毁少年i 2021-01-20 19:39

I want to use allContactedBodies instead of didBeginContact & didEndContact.

When I do :

NSLog(@\"%@\", node.physicsBody.allContactedBodies );
<         


        
3条回答
  •  星月不相逢
    2021-01-20 20:03

    Apparently, when collisions happen very rapidly, contacts with multiple nodes are reported simultaneusly. In this case, only one of the collisions will be detected in didBeginContact, and you may lose the contact you are interested in.

    You can detect simultaneous (or almost simultaneous) contacts in didBeginContact as follow, and use it to apply whatever logic you need.

    NSArray *tempArray = [mySprite.physicsBody allContactedBodies];
    BOOL contactWithNodeOfInterest = NO;
    int i = 0;
    for(SKPhysicsBody *body in tempArray)
       {
        if([body.node.name isEqualToString:@"nodeOfInterest"]) { contactWithNodeOfInterest = YES; }
        NSLog(@"Contacts: %i %@",i,body.node.name);
        i = i + 1;
       }
    

提交回复
热议问题