问题描述
我想使用 allContactedBodies 而不是 didBeginContact &没有结束联系.
I want to use allContactedBodies instead of didBeginContact & didEndContact.
当我这样做时:
NSLog(@"%@", node.physicsBody.allContactedBodies );
并且正确的接触发生在对象上,我得到类似的东西:
And the correct contact happens with the object,I get something like:
< SKPhysicsBody>类型:其中矩形> representedObject:并[d SKNode>名称: 'theBall' 位置:{149.55787658691406,91.00054931640625} accumulatedFrame:{{70.462608337402344,-16.016334533691406},{112.56977081298828,127.18753814697266}}]"
现在我想做的就是说好的,如果你看到名字:'theBall',那么我们已经连接了.所以我尝试执行以下不起作用的代码.
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?
谢谢.
推荐答案
allContactedBodies
属性返回一个 SKPhysicsBody 对象数组.您可以使用 SKPhysicsBody 的 node
属性访问每个physicsBody 所附加的节点
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");
}
在 Swift 中,上面的代码可以写成这样:
In Swift the same code above can be written like:
val ballNode: SKNode? = yourNode.physicsBody.allContactedBodies().first(where { $0.node.name == "theBall" })?.node
这篇关于如何正确使用 allContactedBodies?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!