大家帮我看看代码哪里出了问题,很简单的代码,但是onContactBegin就是没有被调用,研究了半天,实在是没办法了,
只好来求助坛子了。
class HelloWorld : public cocos2d::Layer
{
public:
Sprite *_ball;
PhysicsWorld *m_world;
void setPhyWorld(PhysicsWorld* world){ m_world = world; };
// there’s no ‘id’ in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here’s a difference. Method ‘init’ in cocos2d-x returns bool, instead of returning ‘id’ in cocos2d-iphone
virtual bool init();
bool onContactBegin(PhysicsContact& contact);
// implement the “static create()” method manually
CREATE_FUNC(HelloWorld);
};
Scene* HelloWorld::createScene()
{
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
auto layer = HelloWorld::create();
layer->setPhyWorld(scene->getPhysicsWorld());
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
_ball = Sprite::create("Ball.jpg");
_ball->setPosition(Point(400,600));
auto ballBody = PhysicsBody::createCircle(_ball->getContentSize().width / 2);
_ball->setPhysicsBody(ballBody);
this->addChild(_ball);
auto edgeSp = Sprite::create();
auto boundBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 30);
edgeSp->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
edgeSp->setPhysicsBody(boundBody);
this->addChild(edgeSp);
edgeSp->setTag(0);
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
return true;
}
bool HelloWorld::onContactBegin(PhysicsContact& contact)
{
CCLOG(“onContactBegin”);
return true;
}
运行时,球落地后,bool HelloWorld::onContactBegin(PhysicsContact& contact)没有被调用到。