我不太了解 C++,我正在尝试使用 Cocos2d-box2d实现我找到的解决方案 - ( Get the world's contactListener in Box2D )。这是联系侦听器。
SubcContactListener.h:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import <vector>
typedef std::pair<b2Fixture*, b2Fixture*> fixturePair;
typedef std::vector<fixturePair> thingsThatTouched;
extern thingsThatTouched g_touchingFixtures;
class SubcContactListener : public b2ContactListener {
public:
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
};
SubcContactListener.mm:
#import "SubcContactListener.h"
void SubcContactListener:: BeginContact(b2Contact *contact) {
thingsThatTouched->push_back( make_pair(contact->GetFixtureA(), contact->GetFixtureB()) );
}
void SubcContactListener:: EndContact(b2Contact *contact) {
}
我得到一个
Expected unqualified-id
在线错误
thingsThatTouched.push_back( make_pair(contact->GetFixtureA(), contact->GetFixtureB()) );
在 SubcContactListener.mm 的 BeginContact 方法中。我也得到一个
Unexpected type name 'thingsThatTouched': expected expression
行错误
b2Fixture* fixtureA = thingsThatTouched[i].first;
b2Fixture* fixtureB = thingsThatTouched[i].second;
在 HelloWorldLayer 类的 tick 方法中。
更新:
当两个精灵(有自己的类)碰撞时,我试图创建一个焊接接头。这是我在 HelloWorld.mm 的 tick 方法中所做的:
b2WeldJoint *weldJoint;
b2WeldJointDef weldJointDef;
for (int i = 0; i < touchingBodies.size(); i++) {
b2Body* bodyA = touchingBodies[i].first;
b2Body* bodyB = touchingBodies[i].second;
weldJointDef.Initialize(bodyA, bodyB, bodyA->GetWorldCenter());
weldJointDef.collideConnected = false;
weldJoint = (b2WeldJoint*)world->CreateJoint(&weldJointDef);
}
touchingBodies.clear();
但我收到以下错误:
Apple Mach-O Linker (Id) Error
"_touchingBodies", referenced from:
SubcContactListener::BeginContact(b2Contact*) in SubcContactListener.o