我一直在尝试使用 Ray Wenderlich 教程在 cocos2d 中进行碰撞检测,但是每当我尝试破坏 b2Bodyinline b2Body* b2Fixture::GetBody() { return m_body; }
时,只要对象发生碰撞,我就会得到。我看过类似的问题,但似乎没有一个对我有用。这是我的勾选方法;问题从“std::vector”开始,运行正常之前的代码,只是检测到两个物体之间的碰撞并破坏两个 b2Bodies 每次都会给我一个错误:`
(void)tick:(ccTime) dt {
_world->Step(dt, 10, 10);
...some code that works fine...then here when trying to delete the bodies:
std::set<b2Body *>toDestroy;
std::set<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
bodyA = contact.fixtureA->GetBody();
bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
spriteA = (__bridge CCSprite *) bodyA->GetUserData();
spriteB = (__bridge CCSprite *) bodyB->GetUserData();
if (((spriteA.tag == 411 || spriteA.tag == 412) && spriteB.tag == 8))
{
//Remove CCSprites
[self removeBall];
for(CCSprite *tile4 in row4){
[self removeTiles];
[row4 removeObject:tile4];
[childrenToDestroy addObject:tile4];
}
//Remove b2bodies by adding them to array
toDestroy.insert(bodyA);
toDestroy.insert(bodyB);
}
}
}
std::set<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL)
{
body->SetActive(false);
_world->DestroyBody(body);
}
}
这部分代码看起来是正确的,但随后我在样板文件“ stl_function.h”中收到一个错误,提示“二进制表达式的操作数无效('const MyContact' 和 'const MyContact') ”并且它说它在我的联系人监听器中找到文件:
#import "MyContactListener.h"
MyContactListener::MyContactListener() : _contacts() {
}
MyContactListener::~MyContactListener() {
}
void MyContactListener::BeginContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.insert(myContact); <------------//Says "7.In instantiation of member function 'std::set<MyContact, std::less<MyContact>, std::allocator<MyContact> >::insert' requested here"
}
void MyContactListener::EndContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
std::set<MyContact>::iterator pos;
pos = std::find(_contacts.begin(), _contacts.end(), myContact);
if (pos != _contacts.end()) {
_contacts.erase(pos);
}
}
void MyContactListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) {
}
void MyContactListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
}