cocos2d-x 3.0 使用最新物理引擎的一个源码实例

来源:互联网 发布:淘宝运费险退货多 编辑:程序博客网 时间:2024/05/17 07:44

1.碰撞函数参数由两个变成一个了

2.检测不到碰撞。需要设置那三个参数,同时还要设置成动态的。body进行设置。

3.初始入口文件也发生了改变。

附录上我最近调试好的cocos2d-x 3.1 使用最新物理引擎的一个源码实例,是一个小猫碰撞小车的例子。最新的引擎都可用。

资源文件和代码下载:http://download.csdn.net/detail/u014734779/7417009

部分代码参考:

Scene* HelloWorld::createScene(){    // 'scene' is an autorelease objectauto scene = Scene::createWithPhysics();scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);Vect gravity = Vect(0.0f, 0.0f);scene->getPhysicsWorld()->setGravity(gravity);        // 'layer' is an autorelease object    auto layer = HelloWorld::create();layer->setPhyWorld(scene->getPhysicsWorld());    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }    _spriteSheet = SpriteBatchNode::create("sprites.png", 2);SpriteFrameCache::getInstance()->addSpriteFramesWithFile("sprites.plist", "sprites.png");this->addChild(_spriteSheet);this->spawnCar();this->schedule(schedule_selector(HelloWorld::secondUpadte), 1.0f);this->scheduleUpdate();    return true;}void HelloWorld::spawnCar(){SpriteFrame* frame = SpriteFrameCache::getInstance()->spriteFrameByName("car.png");Sprite* car = Sprite::createWithSpriteFrame(frame);car->setPosition(Point(100, 100));addBoxBodyForSprite(car);car->setTag(2);car->runAction(MoveTo::create(1.0f, Point(300, 300)));car->runAction(RepeatForever::create(Sequence::create(MoveTo::create(1.0,Point(300, 100)),MoveTo::create(1.0,Point(200, 200)),MoveTo::create(1.0,Point(100, 100)),NULL)));_spriteSheet->addChild(car);}void HelloWorld::spawnCat(){auto winSize = Director::getInstance()->getWinSize();auto cat = Sprite::createWithSpriteFrameName("cat.png");int minY = cat->getContentSize().height / 2;int maxY = winSize.height - (cat->getContentSize().height / 2);int rangeY = maxY - minY;int actualY = CCRANDOM_0_1() * rangeY;int startX = winSize.width + (cat->getContentSize().width / 2);int endX = -(cat->getContentSize().width / 2);Point startPos = Point(startX, actualY);Point endPos = Point(endX, actualY);cat->setPosition(startPos);addBoxBodyForSprite(cat);cat->setTag(1);cat->runAction(Sequence::create(MoveTo::create(1.0, endPos), CallFuncN::create(this, callfuncN_selector(HelloWorld::spriteDone)),NULL));_spriteSheet->addChild(cat);}void HelloWorld::addBoxBodyForSprite(Sprite* sprite){auto body = PhysicsBody::createBox(sprite->getContentSize());body->setDynamic(true);body->setCategoryBitmask(1);    // 0001    body->setCollisionBitmask(-1);   // 0001    body->setContactTestBitmask(-1); // 0001  必须要设置,而且设置为setDynamic(true);而且必须是0重力可以解决sprite->setPhysicsBody(body);}void HelloWorld::secondUpadte(float dt){this->spawnCat();}void HelloWorld::spriteDone(Node* sender){Sprite *sprite = (Sprite*)sender;_spriteSheet->removeChild(sprite,true);}void HelloWorld::onEnter(){Layer::onEnter();auto contactListener = EventListenerPhysicsContact::create();contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);auto dispatcher = Director::getInstance()->getEventDispatcher();dispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);}bool HelloWorld::onContactBegin( PhysicsContact& contact){auto spriteA = (Sprite*)contact.getShapeA()->getBody()->getNode();auto spriteB = (Sprite*)contact.getShapeB()->getBody()->getNode();if (spriteA->getTag() == 1){spriteA->removeFromParentAndCleanup(true);}if (spriteB->getTag() == 1){spriteB->removeFromParentAndCleanup(true);}return true;}


0 0