私はC++をあまり知らないので、Cocos2d-box2dを使用して見つけたソリューションを実装しようとしています-(Box2Dで世界のcontactListenerを取得する)。これが連絡先リスナーです。
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メソッドで。
更新しました:
2つのスプライト(独自のクラスを持つ)が衝突したときに、溶接ジョイントを作成しようとしています。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