How do I correctly use allContactedBodies?

前端 未结 3 787
梦毁少年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:19

    The allContactedBodies property returns an array of SKPhysicsBody objects. You can access the node to which each physicsBody is attached by using the node property of SKPhysicsBody

    NSArray *tempArray = [yourNode.physicsBody allContactedBodies];
    for(SKPhysicsBody *body in tempArray)
    {
        if([body.node.name isEqualToString:@"theBall"])
            NSLog(@"found the ball");
    }
    

    In Swift the same code above can be written like:

    val ballNode: SKNode? = yourNode.physicsBody.allContactedBodies().first(where { $0.node.name == "theBall" })?.node
    

提交回复
热议问题