I want to use allContactedBodies instead of didBeginContact & didEndContact.
When I do :
NSLog(@"%@", node.physicsBody.allContactedBodies );
And the correct contact happens with the object,I get something like:
"<SKPhysicsBody> type:<Rectangle> representedObject:[<SKNode> name:'theBall' position:{149.55787658691406, 91.00054931640625} accumulatedFrame:{{70.462608337402344, -16.016334533691406}, {112.56977081298828, 127.18753814697266}}]"
Now all I want to do is to say ok great, if you see the name:'theBall' then we are connected. So I tried to do the following code which doesn't work.
if ([node.physicsBody.allContactedBodies containsObject:@"theBall"] ) {
NSLog(@"Connected");
}
What am I doing wrong? any idea?
Thanks.
If you read the SKPhysicsBody Class Reference, you should have seen the format for this command.
The return value is:
Having said that, you would use this code to accomplish what you are asking:
FYI - You will have to run this code in the
update:
method. This means your app will spend precious processing time every single frame checking for a contact. It would make much more sense to stick with thedidBeginContact:
instead.The
allContactedBodies
property returns an array of SKPhysicsBody objects. You can access the node to which each physicsBody is attached by using thenode
property of SKPhysicsBodyIn Swift the same code above can be written like:
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.