新的物理引擎叫physicsBody
创建物理scene
auto scene = Scene::createWithPhysics();
添加调试信息 这样可以让刚体附加方框显示出来
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
创建边界框
void Stage::addEdgeBox()
{
auto visibleSize = Director::getInstance()->getVisibleSize();
//建立刚体
auto edgeBox = PhysicsBody::createEdgeBox(visibleSize);
//创建图形
auto shape =Node::create();
//刚体附加到图形
shape->setPhysicsBody(edgeBox);
this->addChild(shape);
}
</pre><span style="color: rgb(51, 51, 51); font-family: sans-serif; font-size: 24px;">shape的默认位置是(0,0),但是这个是node的中心位置,我们把它设置到屏幕中心 </span><br style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: sans-serif; font-size: 24px;" /><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; padding-top: 0.2rem; padding-bottom: 0.3rem; font-family: Arial, 'Microsoft YaHei'; font-size: 0.7rem; color: rgb(51, 51, 51); line-height: 0.875rem; word-break: break-all;"></p><pre name="code" class="cpp" style="box-sizing: border-box; font-family: Arial, 'Microsoft YaHei'; font-size: 13px; white-space: pre-wrap; padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; border: 1px solid rgb(204, 204, 204); border-radius: 4px; overflow: hidden; width: 1543px; background-color: rgb(245, 245, 245);">shape->setPosition(visibleSize.width/2,visibleSize.heigh
设置好了世界,现在把精灵player变成刚体,只需要一句话
player->setPhysicsBody(PhysicsBody::createBox(player->getContentSize()));
这时候如果把精灵的position放到屏幕中间,就可以看到精灵下落,因为有了重力,而且到地面还会有反弹。