我已经阅读了很多关于这个问题的帖子,但我还没有找到任何特别有帮助的帖子。当两个精灵使用接触式监听器相遇时,我正在尝试进行焊接。我一直收到以下错误:
Apple Mach-O Linker (Id) Error
"_touchingBodies", referenced from:
SubcContactListener::BeginContact(b2Contact*) in SubcContactListener.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的联系人听众。 SubcContactListener.h:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import <vector>
typedef std::pair<b2Body*, b2Body*> bodyPair;
typedef std::vector<bodyPair> thingsThatTouched;
extern thingsThatTouched touchingBodies;
class SubcContactListener : public b2ContactListener {
public:
void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
};
SubcContactListener.mm:
#import "SubcContactListener.h"
void SubcContactListener:: BeginContact(b2Contact *contact) {
touchingBodies.push_back( std::make_pair(contact->GetFixtureA()->GetBody(), contact->GetFixtureB()->GetBody()) );
}
我补充说:
thingsThatTouched touchingBodies;
到HelloWorldLayer.h接口。
最后,在HelloWorldLayer.mm的tick方法中(在时间步之后):
b2WeldJointDef weldJointDef;
b2WeldJoint *weldJoint;
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();
请帮忙,我已经有一段时间了。
答案 0 :(得分:0)
extern thingsThatTouched touchingBodies;
这些外部变量必须在其他地方定义为静态C变量,而不是实例变量。
鉴于更好的设计,我建议废弃extern变量,然后通过向HelloWorldLayer添加Singleton接口来访问touchBodies。
然后您就可以从任何地方访问它了:
[HelloWorldLayer sharedWorldLayer].touchingBodies;