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 __T32_H__ #define #include USING_NS_CC; #define #define #endif |
2.编写TBack.h |
#ifndef __TBack_H__ #define #include class { public: CREATE_FUNC(TBack); bool }; #endif |
3编写TBack.cpp |
#include "TBack.h" 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 __T06Box2D_H__ #define #include #include class { public: CREATE_FUNC(T06Box2D); bool b2World* b2Body* void }; #endif |
5编写:T06Box2D.cpp |
#include "T06Box2D.h" #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 __TMenu_H__ #define #include class { public: CREATE_FUNC(TMenu); bool bool }; #endif |
7. 编写:TMenu.cpp |
#include "TMenu.h" #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 "AppDelegate.h" #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(); } |
执行效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > |
版权声明:本文博主原创文章,博客,未经同意不得转载。