联系听众问题

时间:2012-07-12 13:43:43

标签: c++ cocos2d-iphone box2d box2d-iphone

我不太了解C ++,我正在尝试使用Cocos2d-box2d实现我找到的解决方案 - (Getting the world's contactListener in Box2D)。这是Contact Listener。

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

1 个答案:

答案 0 :(得分:0)

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

当你说,

typedef int myOwnInt;

现在你不能使用,

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

myOwnInt i = 10; 

希望它有所帮助!