本次的cocos2d-x的版本号为2.1.3,废话不多说,直接上代码!!!!!
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- #include "Box2D/Box2D.h"
- using namespace cocos2d;
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene();
- CREATE_FUNC(HelloWorld);
- HelloWorld ();
- ~HelloWorld ();
- void tick(float time);
- private:
- //世界
- b2World *m_world;
- //刚体
- b2Body *m_body;
- //篮球
- CCSprite *m_ball;
- };
- #endif // __HELLOWORLD_SCENE_H__
- #include "HelloWorldScene.h"
- using namespace cocos2d;
- #define PTM_RATIO 32.0 //像素到米的转换
- HelloWorld :: HelloWorld ()
- {
- m_world = NULL;
- m_body = NULL;
- m_ball = NULL;
- }
- HelloWorld::~HelloWorld()
- {
- delete m_world ;
- m_body = NULL;
- m_ball = NULL;
- }
- CCScene* HelloWorld::scene()
- {
- CCScene * scene = NULL;
- do
- {
- scene = CCScene::create();
- CC_BREAK_IF(! scene);
- HelloWorld *layer = HelloWorld::create();
- CC_BREAK_IF(! layer);
- scene->addChild(layer);
- } while (0);
- return scene;
- }
- bool HelloWorld::init()
- {
- bool bRet = false;
- do
- {
- CC_BREAK_IF(! CCLayer::init());
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- //篮球
- m_ball = CCSprite::create("Ball.png");
- CC_BREAK_IF(!m_ball);
- m_ball->setPosition(ccp(100,400));
- this->addChild(m_ball);
- //创建一个世界
- b2Vec2 gravity(0.0f,-10.0f);
- m_world = new b2World(gravity);
- /** 创建围绕整个屏幕的边缘 **/
- //刚体定义
- b2BodyDef groundBodyDef;
- groundBodyDef.position.Set(0,0);
- //世界根据刚体定义生成刚体
- b2Body *groundBody = m_world->CreateBody(&groundBodyDef);
- //生成形状
- b2EdgeShape groundBox;
- //关联定义
- b2FixtureDef boxShapeDef;
- boxShapeDef.shape = &groundBox;
- //设置形状属性 ---下边框
- groundBox.Set( b2Vec2(0.0f,0.0f),b2Vec2(size.width/PTM_RATIO,0) );
- //创建关联
- groundBody->CreateFixture(&boxShapeDef);
- //设置形状属性 ---左边框
- groundBox.Set( b2Vec2(0.0f,0.0f),b2Vec2(0,size.height/PTM_RATIO) );
- //创建关联
- groundBody->CreateFixture(&boxShapeDef);
- //设置形状属性 ---上边框
- groundBox.Set( b2Vec2(0.0f,size.height/PTM_RATIO),b2Vec2(0,size.height/PTM_RATIO) );
- //创建关联
- groundBody->CreateFixture(&boxShapeDef);
- //设置形状属性 --右边框
- groundBox.Set( b2Vec2(size.width/PTM_RATIO,size.height/PTM_RATIO),
- b2Vec2(size.width/PTM_RATIO,0.0f) );
- //创建关联
- groundBody->CreateFixture(&boxShapeDef);
- //创建篮球刚体与形状
- b2BodyDef ballBodyDef;
- ballBodyDef.type =b2_dynamicBody;
- ballBodyDef.position.Set(100/PTM_RATIO,400/PTM_RATIO);
- //关联篮球精灵
- ballBodyDef.userData = m_ball;
- //创建篮球刚体
- m_body = m_world->CreateBody(&ballBodyDef);
- //生成形状
- b2CircleShape circle;
- circle.m_radius = 26.0/PTM_RATIO;
- //关联定义
- b2FixtureDef ballShapeDef;
- ballShapeDef.shape = &circle;
- //密度
- ballShapeDef.density = 1.0f;
- //摩擦力
- ballShapeDef.friction = 1.0f;
- //反弹系数
- ballShapeDef.restitution = 0.8f;
- m_body->CreateFixture(&ballShapeDef);
- this->schedule(schedule_selector(HelloWorld::tick),0.1f);
- bRet = true;
- } while (0);
- return bRet;
- }
- void HelloWorld::tick(float time)
- {
- //世界步
- m_world->Step(time,10,10);
- //获取世界的里的刚体
- for ( b2Body *body = m_world->GetBodyList();body!=NULL;body=body->GetNext() )
- {
- //获取篮球精灵
- if (body->GetUserData())
- {
- //根据刚体的位置设置篮球精灵的位置
- CCSprite *ballSprite = (CCSprite*)body->GetUserData();
- ballSprite->setPosition( ccp(body->GetPosition().x*PTM_RATIO,
- body->GetPosition().y*PTM_RATIO) );
- ballSprite->setRotation( -1*CC_RADIANS_TO_DEGREES(body->GetAngle()) );
- }
- }
- }
运行结果:
代码已经写得很清晰了,我们总结下,首先创建一个世界,世界通过刚体定义生成刚体,关联把形状连接起来,刚体通过关联定义生成关联!!下面附上篮球一个!!
由于本人是业余,代码难免有所披露,望大家斧正!!谢谢,本人QQ947491240恭候大家拍砖!!再次感谢!!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。