PhysicsBody
添加物理属性,不多废话,直接上代码
Sprite *land = Sprite::createWithSpriteFrame(pngName); PhysicsBody *Landbody = PhysicsBody::create(); Landbody->addShape(PhysicsShapeBox::create( Size(land->getContentSize().width,land->getContentSize().height) ,PhysicsMaterial(1,0,1))); Landbody->setDynamic(false);//我测试的效果是 是否动态(即给他速度或力,他并不会动) Landbody->setLinearDamping(0.0f);//阻力 Landbody->setGravityEnable(false);//是否受重力 Landbody->setContactTestBitmask(1);//只要物体的这个至进行与操作后不为零则触发碰撞 // add the sprite as a child to this layer land->setPhysicsBody(Landbody);
碰撞检测
auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); bool GameLayer::onContactBegin(const PhysicsContact& contact) { return true;//true触发碰撞,false穿过,将碰撞事件传递给下一个检测回调函数 }
cocos2dx-3.2 笔记 - 物理属性
时间: 2024-09-28 06:52:07