0

我不太了解 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
4

1 回答 1

0

thingsThatTouched是一个类型定义。它只是类型的同义词,而不是标识符/变量的同义词。换句话说,它本身不是一个对象。我认为你应该g_touchingFixtures在你使用的地方使用thingsThatTouched.

当你说,

typedef int myOwnInt;

现在不能用了

myOwnInt = 10;   // Wrong. myOwnInt is just a type like how int is.

myOwnInt i = 10; 

希望能帮助到你 !

于 2012-07-12T13:50:38.357 回答