刚开始xcode莫名其妙的报错误,提示box2d没有开启,主要有两个地方需要设置,在targets->buidseting->preprocessing中设置CC_ENABLE_BOX2D_INTEGRATION=1删除climuk,project->buidseting->preprocessing同样但是对于使用,a的同志们来说还是找不到,那就必须从新生成cocos2dlib.a文件,生成之前一定设置以上地方的CC_ENABLE_BOX2D_INTEGRATION=1删除climuk后在生成,然后换上项目中的.a就没问题了
Leave2.h
#ifndef Leave2_hpp
#define Leave2_hpp
#include "GLES-Render.h"
#include <cstdlib>
#include <stdio.h>
#include "ShyRole.h"
#include "Roulette.h"
#include "cocos2d.h"
#include "VisibleRect.h"
#include <Box2D/Box2D.h>
using namespace cocos2d;
class Leave2 : public Scene{
public:
Leave2();
~Leave2();
virtual void draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t flags) override;
virtual bool init() override;
CREATE_FUNC(Leave2);
private:
b2World* world;
void update(float dt) override;
GLESDebugDraw m_debugDraw;
};
#endif /* Leave2_hpp */
Leave2.cpp
#define PTM_RATIO 32
#include "Leave2.h"
#include <Box2D/Box2D.h>
#include "extensions/cocos-ext.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCCustomCommand.h"
Leave2::Leave2()
{
init();
}
Leave2::~Leave2()
{
CC_SAFE_DELETE(world);
}
bool Leave2::init(){
if(!Scene::init()){
return false;
}
//重力参数
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
//物理世界
world = new b2World(gravity);
//是否可以休眠
world->SetAllowSleeping(true);
//开启连续物理测试(避免物体速度快物理穿透)
world->SetContinuousPhysics(true);
//设施debug模式(绘制刚体)
GLESDebugDraw* m_debugDraw = new GLESDebugDraw(PTM_RATIO);
world->SetDebugDraw(m_debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit; //有很多种绘制方式 可以查看源代码
m_debugDraw->SetFlags(flags);
//定义刚体属性(位置类型等b2BodyDef需要添加到b2Body上)
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0);
//刚体身体
b2Body* groundBody = world->CreateBody(&groundBodyDef);
//定义一个有边的形状
b2EdgeShape groundBox;
groundBox.Set(b2Vec2(VisibleRect::leftBottom().x/PTM_RATIO,VisibleRect::leftBottom().y/PTM_RATIO), b2Vec2(VisibleRect::rightBottom().x/PTM_RATIO,VisibleRect::rightBottom().y/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
groundBox.Set(b2Vec2(VisibleRect::leftTop().x/PTM_RATIO,VisibleRect::leftTop().y/PTM_RATIO), b2Vec2(VisibleRect::rightTop().x/PTM_RATIO,VisibleRect::rightTop().y/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
groundBox.Set(b2Vec2(VisibleRect::leftTop().x/PTM_RATIO,VisibleRect::leftTop().y/PTM_RATIO), b2Vec2(VisibleRect::leftBottom().x/PTM_RATIO,V