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
- cocos2d-x 3.0 使用最新物理引擎的一个源码实例
- Cocos2d-x物理引擎世界的使用
- 实例介绍Cocos2d-x物理引擎:使用关节
- 实例介绍Cocos2d-x中Box2D物理引擎:使用关节
- 实例介绍Cocos2d-x中Box2D物理引擎:使用关节
- Cocos2d-x 3.0 简捷的物理引擎
- Cocos2d-x-3. X使用自带的物理引擎
- 实例介绍Cocos2d-x物理引擎:HelloPhysicsWorld
- 在cocos2d-x 3.0中使用物理引擎
- 【Cocos2d-x】物理引擎使用入门
- cocos2d-x物理引擎
- cocos2d-x物理引擎
- cocos2d-x 物理引擎
- 使用Cocos2d-x 3.0和物理引擎制作简单的platformer游戏
- 关于cocos2d-X 3.x版本使用引擎自带的物理引擎Physics
- Cocos2d-x 3.0 物理引擎小demo
- 实例介绍Cocos2d-x物理引擎:碰撞检测
- 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D
- [Digital][Hardware][PC]AX Series Computers Released[VCSA][2014.06.28]
- 微信公众平台会火的原因
- 接口、抽象多态等部分试题
- java保留两位小数4种方法
- 模拟-hdoj-4831-百度之星2014初赛第二场
- cocos2d-x 3.0 使用最新物理引擎的一个源码实例
- 借助虚继承来实现不允许继承的类
- 翻译Activity
- 用sublime生成html网页头部代码的快捷方式
- oracle集合操作符
- 豌豆荚笔试题
- 主流PCB软件对比(转自于博士)
- 编写String类的构造函数,析构函数,赋值运算符
- MySQL中ONDUPLICATEKEYUPDATE 介绍