didBeginContact - bodyA & bodyB end Contact if one of them hits another category

流过昼夜 提交于 2019-12-13 03:55:28

问题


Here is the deal, I've got a basket, an Apple and an Orange bodies. We are going to drop them into the basket.

I also want to know the presence of the Apple inside the basket, so I go ahead and add a didEndContact method.

Lets assume the apple is already inside the basket. Now I drop the Orange in the basket, everything works fine until the Orange hits both the Apple and the surface which is the basket.

As soon as the Orange hits the Apple while it's seating in the basket, Apple category ends contact with basket category and therefore we get ballOneIntTheBasket = NO. bodyA becomes orange category and bodyB becomes basket category

How do I get apple category to stay connected with all the connected bodies?

didBeginContact code

if (bodyA == appleCategory && bodyB == basketCategory) {
appleInTheBasket= YES;
}

if (bodyA == orangeCategory && bodyB == basketCategory) {
orangeInTheBasket = true;
}

didEndContact code

if (bodyA == appleCategory && bodyB == basketCategory) {
appleInTheBasket = NO;
}

回答1:


Instead of using the contact delegate for this, you can use the allContactedBodies property of SKPhysicsBody.

So, using

basket.physicsBody.allContactedBodies

will return you a NSArray of all SKPhysicsBody objects currently in contact with the basket.

You can use this property in the -update: method to monitor the basket.



来源:https://stackoverflow.com/questions/23776401/didbegincontact-bodya-bodyb-end-contact-if-one-of-them-hits-another-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!