我为联系人创建了一个EventListener并将其添加到我正在处理的图层,两个PhysicsBody或PhysicsBody和PhysicsEdgeBody之间的PhysicsContact和Collisions工作得很好,但是它没有两个PhysicsEdgeBody它没有#&## 39;完全没有工作。
已经尝试过:
但仍无效。
我有什么遗失的东西吗?
以下是使用以下内容创建的GameScene示例: Scene::createWithPhysics()
还添加了命令: scene->getPhysicsWorld()->setDebugDrawMask(DEBUGDRAW_ALL);
//Node and PhysicsBody creation
PhysicsBody* bodyA = PhysicsBody::createEdgeBox(someSize, somePhysicsMaterial);
bodyA->setDynamic(true);
bodyA->setMass(100);
bodyA->setCategoryBitmask(0X01); //0001
bodyA->setCollisionBitmask(0X02); //0010
bodyA->setContactTestBitmask(0X02); //0010
PhysicsBody* bodyB = PhysicsBody::createEdgeSegment(Vec2::ZERO, Vec2(visibleSize.width, 0), somePhysicsMaterial);
bodyB->setDynamic(true);
bodyB->setMass(100);
bodyB->setCategoryBitmask(0X02); //0010
bodyB->setCollisionBitmask(0X01); //0001
bodyB->setContactTestBitmask(0X01); //0001
Node* nodeA = Node::create();
nodeA->setPosition(visibleCenter);
nodeA->setPhysicsBody(bodyA);
Node* nodeB = Node::create();
nodeB->setPosition(Vec2::ZERO);
nodeB->setPhysicsBody(bodyB);
this->addChild(nodeA);
this->addChild(nodeB);
//EventListener creation
auto cListener = EventListenerPhysicsContact::create();
cListener->onContactBegan = CC_CALLBACK__1(GameScene::onContactBegan, this);
cListener->onContactPreSolve = CC_CALLBACK_"(GameScene::onContactPreSolve, this);
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(cListener, this);
即使两个实体在屏幕上发生碰撞,也不会调用此回调。
//onContactBegan Callback
bool GameScene::onContactBegan(PhysicsContact &contact)
{
PhysicsBody *bodyA = contact.getShapeA()->getPhysicsBody();
PhysicsBody *bodyB = contact.getShapeB()->getPhysicsBody();
if ((bodyA->getCategoryBitmask() == 0X01 && bodyB->getCategoryBitmask() == 0X02) ||
(bodyA->getCategoryBitmask() == 0X02 && bodyB->getCategoryBitmask() == 0X01))
{
CCLOG("COLLISION");
return true;
}
return false;
}