2

2つのボディ間に接触が確立されたときに溶接ジョイントを作成する接触リスナーを設定しました。体が触れると、線を指すSIGABRTが表示され続けます

b2Assert(IsLocked() == false);

メソッドで

b2Joint* b2World::CreateJoint(const b2JointDef* def)

クラス内

b2World.cpp

連絡先リスナークラスは次のとおりです。SubcContactListener.h:

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import "Spriter.h"
#import "CreateRope.h"

class SubcContactListener : public b2ContactListener    {
public:
void     *helloWorldLayer;

b2WeldJoint *weldJoint;
b2World *world;

void BeginContact(b2Contact* contact);
void EndContact(b2Contact* contact);
void createWeldJoint(b2Body* ABody ,b2Body* BBody);
//void destroyWeldJoint(b2WeldJoint *weldJoint);

};

SubcContactListener.mm:

#import "SubcContactListener.h"
#import "HelloWorldLayer.h"


void SubcContactListener:: BeginContact(b2Contact *contact) {

}

void SubcContactListener:: EndContact(b2Contact *contact)   {
b2Fixture       *fixtureA = contact->GetFixtureA();
b2Fixture       *fixtureB = contact->GetFixtureB();
b2Body          *fixtureABody = fixtureA->GetBody();
b2Body          *fixtureBBody = fixtureB->GetBody();

// We don't care about collisions that don't involve two bodies.
if (helloWorldLayer && fixtureABody && fixtureBBody)
{

    if(fixtureABody != NULL && fixtureBBody != NULL){
        createWeldJoint(fixtureABody, fixtureBBody);
    }

}

}
void SubcContactListener:: createWeldJoint(b2Body* ABody ,b2Body* BBody)    {

// The sprite tag is 1 for the spriter sprite and 3 for the rope links.
if (ABody && BBody)
//{
    CCSprite            *bodyASprite = (CCSprite *) ABody->GetUserData();
    CCSprite            *bodyBSprite = (CCSprite *) BBody->GetUserData();
    NSInteger           bodyASpriteTag = bodyASprite.tag;
    NSInteger           bodyBSpriteTag = bodyBSprite.tag;

    if (((bodyASpriteTag == 1) && (bodyBSpriteTag == 3)) ||
        ((bodyASpriteTag == 3) && (bodyBSpriteTag == 1)))
    {

        //creation of weldjoint
        if ( ABody!= NULL && BBody != NULL) {


        b2WeldJointDef      weldJointDef;
        weldJointDef.Initialize(ABody,
                                BBody,
                                ABody->GetWorldCenter());
        weldJointDef.collideConnected = false;

        weldJoint = (b2WeldJoint*) ABody->GetWorld()->CreateJoint(&weldJointDef);
        }
    }
}

}

また、HelloWorldLayer.mmのinitPhysicsメソッド内:

// Create contact listener
contactListener = new SubcContactListener();
contactListener->helloWorldLayer = self;
world->SetContactListener(contactListener);

エラーから、それは溶接継手と関係があると思いますが、あまりよくわかりません。助けてください。

4

1 に答える 1

1

コールバック内でワールドを変更することはできません。assert ステートメントは、シミュレーション ステップを開始するためにワールドをロックしたため、その操作を実行できないことを示しています。

この問題は、接触イベントをキャッシュし、シミュレーション ステップの後に処理することで解決されます。

于 2012-11-17T04:52:13.723 に答える