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 __T32_H__
#include "cocos2d.h" USING_NS_CC;
#define winSize Director::getInstance()->getWinSize() #define cclog @H_667_419@cocos2d::log
#endif |
2.编写TBack.h |
#ifndef __TBack_H__ #define __TBack_H__
#include "T32.h"
class @H_667_419@TBack : public @H_667_419@Layer { public: CREATE_FUNC(@H_667_419@TBack);
bool init(); };
#endif |
3编写TBack.cpp |
#include "TBack.h"
bool @H_667_419@TBack::init() { @H_667_419@Layer::init();
setLocalZOrder(100);
@H_667_419@Menu* menu = @H_667_419@Menu::create();
@H_667_419@MenuItemImage* item = @H_667_419@MenuItemImage::create("Closenormal.png","CloseSelected.png", [](@H_667_419@Ref*){ popScene(); });
menu->addChild(item); item->setposition(winSize.wIDth / 2 - item->getBoundingBox().size.wIDth / 2, item->getBoundingBox().size.height / 2 - winSize.height / 2);
addChild(menu);
return true; } |
4.编写T06Box2D.h |
__T06Box2D_H__ #define __T06Box2D_H__ #include "T32.h" #include "Box2D/Box2D.h"
class @H_667_419@T06Box2D : public @H_667_419@T06Box2D); bool init();
@H_667_419@b2World* _world; @H_667_419@b2Body* _bat; voID update(float); };
#endif |
5编写:T06Box2D.cpp |
"T06Box2D.h"
#define PTM_RATIO 32.0f
bool @H_667_419@T06Box2D::init();
//创建世界,后面的-9.8表示向下的重力加速度为9.8 //b2Vec2 gravity(0,-9.8f); //这个表示没有重力加速度 @H_667_419@b2Vec2 gravity(0,0.0f); _world = new @H_667_419@b2World(gravity);
{ @H_667_419@b2BodyDef def; //这里是一个动态的body,默认是静态的body def.type = b2_dynamicBody; //设置位置,要转换成重力场中的位置要除以PTM_RATIO def.position.@H_667_419@Set(winSize.wIDth / 2 / PTM_RATIO,winSize.height / 2 / PTM_RATIO);
@H_667_419@b2Body* body = _world->CreateBody(&def);
//让body受力 body->SetlinearVeLocity(@H_667_419@b2Vec2(10,20));
//显示body的精灵 @H_667_419@Sprite* sprite = @H_667_419@Sprite::"Closenormal.png"); addChild(sprite); sprite->setposition(body->Getposition().x*body->Getposition().y*PTM_RATIO);
//设置body的形状,让它和sprite相一致,是圆形的 @H_667_419@b2CircleShape shape; //设置半径 shape.m_radius = sprite->getContentSize().wIDth / 2 / PTM_RATIO; //后面的一个参数表示的是密度系数 @H_667_419@b2Fixture* fixture = body->CreateFixture(&shape,1.0f); //设置摩擦系统 fixture->SetFriction(0.0f); //d性系数 fixture->SetRestitution(1.0f);
//关联body和精灵 body->SetUserData(sprite); }
//加个地板 { @H_667_419@b2BodyDef def; // def.position.Set(0,0);
CreateBody(&def); //设置边界类型的形状 @H_667_419@b2EdgeShape shape; //设置地板的开始点和结束点 shape.@H_667_419@Set(@H_667_419@b2Vec2(0,0),@H_667_419@b2Vec2(winSize.wIDth /
//设置摩擦系数 fixture->SetFriction(0.0f); //设置d性系数 fixture->SetRestitution(1.0f); }
//加个天花板 { @H_667_419@b2BodyDef def; def.position.@H_667_419@Set(0,138); Font-family:新宋体; Font-size:9.5pt">winSize.height / PTM_RATIO);
CreateBody(&def); @H_667_419@b2EdgeShape shape; shape.
//摩擦系统 fixture->SetRestitution(1.0f); }
//左挡板 { @H_667_419@b2BodyDef def; //def.position.Set(0,winSize.height / PTM_RATIO);
PTM_RATIO));
fixture->SetFriction(0.0f); //摩擦系统 fixture->SetRestitution(1.0f); //d性系数 }
//右挡板 {
//摩擦系数 fixture->SetRestitution(1.0f); }
//球拍 { winSize.height / 4 / CreateBody(&def); _bat = body;
"bat.png"); body->SetUserData(sprite); Size batSize = @H_667_419@Size(100,30); @H_667_419@Size content = sprite->getContentSize(); sprite->setScale(batSize.wIDth / content.wIDth,batSize.height / content.height);
@H_667_419@b2polygonShape shape; shape.SetAsBox(batSize.wIDth / 2 / batSize.height / 2 / //摩擦系统 fixture->SetFriction(0.0f); //d性系统 fixture->SetRestitution(1.0f);
//touch @H_667_419@EventListenertouchOneByOne* ev = @H_667_419@EventListenertouchOneByOne::create(); ev->ontouchBegan = [](@H_667_419@touch*,133); Font-family:新宋体; Font-size:9.5pt">Event*){return true; }; ev->ontouchmoved = [&](@H_667_419@touch* touch,133); Font-family:新宋体; Font-size:9.5pt">Event*){ float dx = touch->getDelta().x / PTM_RATIO;
@H_667_419@b2Vec2 pos = _bat->Getposition(); pos.x += dx;
//下面的函数等价于setposition() _bat->Settransform(pos,0); };
_eventdispatcher->addEventListenerWithSceneGraPHPriority(ev,this); }
scheduleUpdate(); return true; }
voID update(float dt) { //时间在流逝 _world->Step(dt,8,1);
//遍历这个世界的body GetbodyList(); while (body) { //设置body相关的精灵的位置 @H_667_419@Sprite* sprite = (@H_667_419@Sprite*)body->GetUserData(); if (sprite) { sprite->PTM_RATIO); sprite->setRotation(body->GetAngle()*180.0 / M_PI); }
body = body->GetNext(); } }
|
6.编写TMenu.h |
__TMenu_H__ #define __TMenu_H__
#include TMenu : public @H_667_419@TMenu);
bool init();
bool touchBegan(@H_667_419@Event*); };
#endif |
7. 编写:TMenu.cpp |
"TMenu.h" #include "TBack.h" #include "T01CPP11.h" #include "T02Vector.h" #include "T03Map.h" #include "T04Label.h" #include "T06Box2D.h"
static const char* Title[] = { "T01CPP11", "T02Vector",21); Font-family:新宋体; Font-size:9.5pt">"T03Map",21); Font-family:新宋体; Font-size:9.5pt">"T04Label",21); Font-family:新宋体; Font-size:9.5pt">"T06Box2D" };
bool @H_667_419@TMenu::create(); addChild(menu);
for (int i = 0; i < sizeof(Title) / sizeof(*Title); ++i) { @H_667_419@MenuItemFont* item = @H_667_419@MenuItemFont::create(Title[i],[](@H_667_419@Ref* sender){
@H_667_419@MenuItem* item = (@H_667_419@MenuItem*)sender; int i = item->getTag()-1000; @H_667_419@Layer* l = NulL;
if (Title[i] == "T01CPP11") l = @H_667_419@T01CPP11::create(); if (Title[i] == "T02Vector") l = @H_667_419@T02Vector::"T03Map") l = @H_667_419@T03Map::"T04Label") l = @H_667_419@T04Label::"T06Box2D") l = create();
if (l) { @H_667_419@TBack* b = create(); @H_667_419@Scene* s = @H_667_419@Scene::create(); s->addChild(b); s->addChild(l); pushScene(s); } }); menu->setTag(1000 + i); }
menu->alignItemsvertically();
// 触摸 auto ev = create(); #if 0 ev->ontouchBegan = [](@H_667_419@Event*){ return true; }; #endif
//ev->ontouchBegan = std::bind(&TMenu::touchBegan,this,std::placeholders::_1,std::placeholders::_2);
ev->ontouchBegan = CC_CALLBACK_2(touchBegan,this);
ev->ontouchmoved = [&](@H_667_419@Event*){ setpositionY(getpositionY() + touch->getDelta().y); }; _eventdispatcher->this);
return true; }
bool touchBegan(/*TMEnu* this,*/@H_667_419@Event*) { return true; } |
8.编写AppDelegate.cpp |
"AppDelegate.h" #include "TBack.h" USING_NS_CC;
@H_667_419@AppDelegate::@H_667_419@AppDelegate() {
}
@H_667_419@AppDelegate::~@H_667_419@AppDelegate() { }
bool @H_667_419@AppDelegate::applicationDIDFinishLaunching() { // initialize director auto director = getInstance(); auto glvIEw = director->getopenGLVIEw(); if(!glvIEw) { glvIEw = @H_667_419@GLVIEw::"My Game"); glvIEw->setFrameSize(480,320); director->setopenGLVIEw(glvIEw); }
glvIEw->setDesignResolutionSize(480,320,133); Font-family:新宋体; Font-size:9.5pt">ResolutionPolicy::EXACT_FIT);
// 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 / 60);
// create a scene. it's an autorelease object auto scene = create(); scene->addChild(create()); scene->create());
// run director->runWithScene(scene);
return true; }
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too voID applicationDIDEnterBackground() { stopAnimation();
// if you use SimpleAudioEngine,it must be pause // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); }
// this function will be called when the app is active again voID applicationWillEnterForeground() { startAnimation();
// if you use SimpleAudioEngine,it must resume here // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); }
|
运行效果:
|
以上是内存溢出为你收集整理的2.Cocos2dx 3.2中的重力系统Box2D全部内容,希望文章能够帮你解决2.Cocos2dx 3.2中的重力系统Box2D所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)