1
添加Box2D相关的库
步骤1:右击项目所在的解决方案à添加—>现有项目àE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\external\Box2D\proj.win32\Box2D.vcxproj
步骤2:右击项目à生成依赖项à项目依赖项à将关于libBox2D的复选框选中
步骤3:为项目添加libBox2D的库
方法:右击项目à属性à链接器à输入—>附加依赖项à编辑,添加上libBox2d.lib,à确定
案例说明:
1.编写T32.h |
#ifndef #define #include USING_NS_CC; #define #define #endif |
2.编写TBack.h |
#ifndef #define #include class { public: CREATE_FUNC(TBack); bool }; #endif |
3编写TBack.cpp |
#include bool { Layer::init(); setLocalZOrder(100); Menu* MenuItemImage* [](Ref*){ Director::getInstance()->popScene(); }); menu->addChild(item); item->setPosition(winSize.width item->getBoundingBox().size.height addChild(menu); return } |
4.编写T06Box2D.h |
#ifndef #define #include #include class { public: CREATE_FUNC(T06Box2D); bool b2World* b2Body* void }; #endif |
5编写:T06Box2D.cpp |
#include #define bool { Layer::init(); //创建世界,后面的-9.8表示向下的重力加速度为9.8 //b2Vec2 gravity(0,-9.8f); //这个表示没有重力加速度 b2Vec2 _world = { b2BodyDef //这里是一个动态的body,默认是静态的body def.type //设置位置,要转换成重力场中的位置要除以PTM_RATIO def.position.Set(winSize.width b2Body* //让body受力 body->SetLinearVelocity(b2Vec2(10,20)); //显示body的精灵 Sprite* addChild(sprite); sprite->setPosition(body->GetPosition().x*PTM_RATIO, //设置body的形状,让它和sprite相一致,是圆形的 b2CircleShape //设置半径 shape.m_radius //后面的一个参数表示的是密度系数 b2Fixture* //设置摩擦系统 fixture->SetFriction(0.0f); //弹性系数 fixture->SetRestitution(1.0f); //关联body和精灵 body->SetUserData(sprite); } //加个地板 { b2BodyDef // def.position.Set(0, 0); b2Body* //设置边界类型的形状 b2EdgeShape //设置地板的开始点和结束点 shape.Set(b2Vec2(0, b2Fixture* //设置摩擦系数 fixture->SetFriction(0.0f); //设置弹性系数 fixture->SetRestitution(1.0f); } //加个天花板 { b2BodyDef def.position.Set(0, b2Body* b2EdgeShape shape.Set(b2Vec2(0, b2Fixture* //摩擦系统 fixture->SetFriction(0.0f); //弹性系数 fixture->SetRestitution(1.0f); } //左挡板 { b2BodyDef //def.position.Set(0, winSize.height / PTM_RATIO); b2Body* b2EdgeShape shape.Set(b2Vec2(0, b2Fixture* fixture->SetFriction(0.0f); fixture->SetRestitution(1.0f); } //右挡板 { b2BodyDef def.position.Set(winSize.width b2Body* b2EdgeShape shape.Set(b2Vec2(0, b2Fixture* //摩擦系数 fixture->SetFriction(0.0f); //弹性系数 fixture->SetRestitution(1.0f); } //球拍 { b2BodyDef def.position.Set(winSize.width b2Body* _bat = Sprite* body->SetUserData(sprite); addChild(sprite); sprite->setPosition(body->GetPosition().x*PTM_RATIO, Size Size sprite->setScale(batSize.width b2PolygonShape shape.SetAsBox(batSize.width b2Fixture* //摩擦系统 fixture->SetFriction(0.0f); //弹性系统 fixture->SetRestitution(1.0f); //touch EventListenerTouchOneByOne* ev->onTouchBegan ev->onTouchMoved float b2Vec2 pos.x //下面的函数等价于setPosition() _bat->SetTransform(pos, }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, } scheduleUpdate(); return } void { //时间在流逝 _world->Step(dt, //遍历这个世界的body b2Body* while (body) { //设置body相关的精灵的位置 Sprite* if (sprite) { sprite->setPosition(body->GetPosition().x*PTM_RATIO, sprite->setRotation(body->GetAngle()*180.0 } body = } } |
6.编写TMenu.h |
#ifndef #define #include class { public: CREATE_FUNC(TMenu); bool bool }; #endif |
7. |
#include #include #include #include #include #include #include static "T01CPP11", "T02Vector", "T03Map", "T04Label", "T06Box2D" }; bool { Layer::init(); Menu* addChild(menu); for (int { MenuItemFont* MenuItem* int Layer* if (title[i] if (title[i] if (title[i] if (title[i] if (title[i] if (l) { TBack* Scene* s->addChild(b); s->addChild(l); Director::getInstance()->pushScene(s); } }); menu->addChild(item); item->setTag(1000 } menu->alignItemsVertically(); // auto #if 0 ev->onTouchBegan return }; #endif //ev->onTouchBegan = std::bind(&TMenu::TouchBegan, this, std::placeholders::_1, std::placeholders::_2); ev->onTouchBegan ev->onTouchMoved setPositionY(getPositionY() }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, return } bool { return } |
8.编写AppDelegate.cpp |
#include #include #include USING_NS_CC; AppDelegate::AppDelegate() } AppDelegate::~AppDelegate() { } bool // initialize director auto auto if(!glview) glview = glview->setFrameSize(480, director->setOpenGLView(glview); } glview->setDesignResolutionSize(480, // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 // create a scene. it's an autorelease object auto scene->addChild(TMenu::create()); scene->addChild(TBack::create()); // run director->runWithScene(scene); return } // This function will be called when the app is inactive. When comes a phone call,it's be invoked too void Director::getInstance()->stopAnimation(); // if you use SimpleAudioEngine, it must be pause // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } // this function will be called when the app is active again void Director::getInstance()->startAnimation(); // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); } |
运行效果: |