cocos2d-x v3.2 FlappyBird 各個類對象具體程式碼分析(1)

來源:互聯網
上載者:User

標籤:cocos2d-x   c++   遊戲   設計   移動   

好久沒寫部落格了,今天早上起來不知怎麼的就開啟了csdn,工作簡曆也投了好幾家,都石沉大海,在學校也閑著沒事,就打算把以前寫的Flappy Bird裡面的一些程式碼分析發出來。

Flappy Bird 網上也有很多的教程,我其中的有些代碼也是參考網上的,不能算是完全原創吧,但是也有很大一部分代碼都是按照我自己的想法,設計寫出來的,接觸cocos2d-x已經差不多有一年了,從以前的2x到現在的3x,改變了很多東西,作為一個新手我還有很多東西要去學習。

開始前有幾點注意事項要說明:

1)我的Flappy Bird 資源使用方法和網上的有所不一樣,具體參照我的以前的一篇部落格。

2)部分代碼參考http://blog.csdn.net/column/details/flappybird.html 尊重原作者

3)我也菜鳥,可能部分代碼寫的不怎麼樣,不嚴謹,請大家見諒(大家互勉嘛)

4)很多代碼的分析都是在注釋裡,也有可能寫在外面。

第一篇文章先介紹這個遊戲中一些不怎麼重要的類和標頭檔:

這個標頭檔主要是定義了一些經常使用的常量,方便測試和修改。

//define.h#pragma once#define BIRDANIMATION_0"birdAnimation_0"//黃色小鳥動畫#define BIRDANIMATION_1"birdAnimation_1"//藍色小鳥動畫#define BIRDANIMATION_2"birdAnimation_2"//紅色小鳥動畫#define BIRD_FLY_FRE0.1f//小鳥翅膀頻率#define LOADING_NUM2//loading 動畫次數#define LOADING_FRE0.5f//loading 動畫頻率#define BIRD_RADIUS15//鳥的半徑#define GRAVITYPoint(0,-980)//重力加速度#define VELOCITYPoint(0,260)//小鳥點擊向上飛速度#define THROUGH_HEIGHT120//小鳥通過兩個管子間的高度#define PIPE_UP1001//向上管子的tag#define PIPE_DOWN1002//向下管子的tag#define PIPE_RANGE200//管子移動Y值的範圍#define PIPE_X300//管子的開始移動的X值#define PIPE_Y-170//管子開始移動的最小Y值#define PIPE_TIME3.0f//管子運動的時間#define PIPE_VELOCITYPoint(-360,0)//管子運動的距離及方向#define LAND_VELOCITY1/60//草地的移動速度#define PIPE_FRE1.5f//管子出現的頻率#define BIRD_DIE_ROTATION90//小鳥死亡後的角度#define SCORECARD_SHOW_TIME0.6f//計分板出現動畫時間#define ADDSCORE_FRE0.05f//加分頻率#define CHANGESCENE_TIME0.5f//情境切換時間#define MEDALS_05//獎盃1分數#define MEDALS_110//獎盃2分數#define MEDALS_215//獎盃3分數#define MEDALS_320//獎盃4分數

下面這個類就不分析了,懂cocos2d-x的都懂

//AppDelegate.h#include "AppDelegate.h"#include "HelloWorldScene.h"#include "WelcomeScene.h"USING_NS_CC;AppDelegate::AppDelegate() {}AppDelegate::~AppDelegate() {}bool AppDelegate::applicationDidFinishLaunching() {    // initialize director    auto director = Director::getInstance();    auto glview = director->getOpenGLView();    if(!glview) {//cocos2d-x 3.2 這個類也變了很多,下面這個語句是定義了視窗的名字,大小,和縮放比例        glview = GLView::createWithRect("FlappyBird",Rect(0,0,286,512),1.0f);        director->setOpenGLView(glview);    }//這個算是簡單的解析度適配.....其他的代碼都是3.2內建的,這裡就不解釋了glview->setDesignResolutionSize(268,512,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 = HelloWorld::createScene();auto scene=WelcomeScene::createScene();    // 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 toovoid AppDelegate::applicationDidEnterBackground() {    Director::getInstance()->stopAnimation();    // if you use SimpleAudioEngine, it must be pause    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();}// this function will be called when the app is active againvoid AppDelegate::applicationWillEnterForeground() {    Director::getInstance()->startAnimation();    // if you use SimpleAudioEngine, it must resume here    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();}

好了,今天就簡單的這兩個檔案了,下篇文章更新是什麼時候我也不確定........

cocos2d-x v3.2 FlappyBird 各個類對象具體程式碼分析(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.