您好,登錄后才能下訂單哦!
本次的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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。