Cocos2d-x
【豆豆跑酷】
豆豆人加跑酷,迸出新滋味!
【遊戲連結】
【Github】
【粒子特效系統】
實作粒子特效編輯器,可自由調整參數、圖片,或使用特製的形狀和煙火
【Github】
【Box 2D】
用Box 2D做的遊戲關卡
【Github】
【筆記】
由於Cocos2d-x只包含最基本的函式庫,所以很多地方全部都要靠程式來完成
- 按鈕事件: 
 在初始化的時候監聽- buttonEventHelloWorldScene.cpp - 1 
 2
 3- // Button 
 c3btn.playbtn = dynamic_cast<Button*>(rootNode->getChildByName("PlayBtn"));//指定按鈕
 c3btn.playbtn->addTouchEventListener(CC_CALLBACK_2(HelloWorld::PlayBtnTouchEvent, this));- 實作按鈕事件 - buttonEventHelloWorldScene.cpp - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20- void HelloWorld::PlayBtnTouchEvent(Ref *pSender, Widget::TouchEventType type) 
 {
 switch (type)
 {
 case Widget::TouchEventType::BEGAN:
 log("Touch Down");
 break;
 case Widget::TouchEventType::MOVED:
 log("Touch Move");
 break;
 case Widget::TouchEventType::ENDED:
 log("Touch Up");
 break;
 case Widget::TouchEventType::CANCELED:
 log("Touch Cancelled");
 break;
 default:
 break;
 }
 }
- 粒子特效:利用兩個串列來管理,分為可以用的和正在用的 - CParticleSystemCParticleSystem.h - 1 
 2- list<CParticle*> _FreeList; 
 list<CParticle*> _InUsedList;- 初始化 - CParticleSystemCParticleSystem.cpp - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12- void CParticleSystem::init(cocos2d::Layer &inlayer) 
 {
 _iFree = NUMBER_PARTICLES;
 _iInUsed = 0;
 _pParticles = new CParticle[NUMBER_PARTICLES]; // 取得所需要的 particle 空間
 // 讀入儲存多張圖片的 plist 檔
 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("particletexture.plist");
 for (int i = 0; i < NUMBER_PARTICLES; i++) {
 _pParticles[i].setParticle("flare.png", inlayer);
 _FreeList.push_front(&_pParticles[i]);
 }
 }- 產生分子並更新串列,最後檢查將已經到達生命週期的分子放回_FreeList - CParticleSystemCParticleSystem.cpp - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33- void CParticleSystem::doStep(float dt) 
 {
 CParticle *get;
 list <CParticle *>::iterator it;
 if (_bEmitterOn) { // 根據 Emitter 設定的相關參數,產生相對應的分子
 int n = (int)(_fElpasedTime * _iNumParticles); // 到目前為止應該產生的分子個數
 if (n > _iGenParticles) {
 for (int i = 0; i < n - _iGenParticles; i++) {
 // 根據 Emitter 的相關參數,設定所產生分子的參數
 if (_iFree != 0) {
 if (_iEmitterType == 0) {
 get = _FreeList.front();
 _FreeList.pop_front();
 _InUsedList.push_front(get);
 _iFree--; _iInUsed++;
 }
 }
 }
 _iGenParticles = n; // 目前已經產生 n 個分子
 }
 }
 // 有分子需要更新時
 if (_iInUsed != 0) {
 for (it = _InUsedList.begin(); it != _InUsedList.end(); ) {
 if ((*it)->doStep(dt)) { // 分子生命週期已經到達
 _FreeList.push_front((*it));// 將目前這一個節點的內容放回 _FreeList
 it = _InUsedList.erase(it); // 移除目前這一個,
 _iFree++; _iInUsed--;
 }
 else it++;
 }
 }
 }
- Joint使用:內建很多種類型,主要就是設定多個物體,最後將他們綁在一起產生關節 - jointJointScene.cpp - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29- // 取得並設定 circle01_pul 為【動態物體A】 
 auto circleSprite = _csbRoot->getChildByName("circle01_pul");
 //設定細節
 Point locA = circleSprite->getPosition();
 Size size = circleSprite->getContentSize();
 float scale = circleSprite->getScale();
 b2CircleShape circleShape;
 circleShape.m_radius = size.width*0.5f*scale / PTM_RATIO;
 b2BodyDef bodyDef;
 bodyDef.type = b2_dynamicBody;
 bodyDef.position.Set(locA.x / PTM_RATIO, locA.y / PTM_RATIO);
 bodyDef.userData = circleSprite;
 b2Body* bodyA = _b2World->CreateBody(&bodyDef);
 b2FixtureDef fixtureDef;
 fixtureDef.shape = &circleShape;
 fixtureDef.density = 10.0f;
 fixtureDef.friction = 0.2f;
 bodyA->CreateFixture(&fixtureDef);
 // 取得並設定 circle02_pul 為【動態物體B】(略)
 //產生滑輪關節
 b2PulleyJointDef JointDef;
 JointDef.Initialize(bodyA, bodyB,
 b2Vec2( locA.x / PTM_RATIO, (locA.y +150) / PTM_RATIO),
 b2Vec2( locB.x / PTM_RATIO, (locB.y +150) / PTM_RATIO),
 bodyA->GetWorldCenter(),
 bodyB->GetWorldCenter(),
 1);
 _b2World->CreateJoint(&JointDef);
 

