2014年12月21日 星期日

[Android開發] Cocos2d-x 檔案架構 (1)


 
檔案結構

打開專案,可分幾個主要資料夾

Classes : 置放主要的遊戲程式碼
Resource : 置放遊戲所需資源,如圖片,音效等等
cocos2d : 函式庫

proj.win32 : Windows架構的建置資料 (啟動c++編輯的xxx.sln檔案也放置於此)
proj.android : Android架構的
proj.ios_mac : IOS架構
proj.linux : Linux架構
proj.wp8-xaml : WindowsPhone架構




















主要檔案

打開Class資料夾,有幾個預設的檔案

AppDelegate.h : 宣告函式
AppDelegate.cpp : 入口函式

HelloWorldScene.h : 宣告函式
HelloWorldScene.cpp :  預設的場景,(包含一張圖片跟關閉按鈕)






AppDelegate.h

//AppDelegate.h 分析------- 

#ifndef _APP_DELEGATE_H_   //如果沒定義
#define _APP_DELEGATE_H_   //則定義
#include "cocos2d.h"   //載入cocos2d.h

class AppDelegate : private cocos2d::Application  //繼承入口函式
{
public:
AppDelegate();
virtual ~AppDelegate();

virtual bool applicationDidFinishLaunching();   //遊戲啟動後的工作 (入口)
virtual void applicationDidEnterBackground();   //待機狀態時調用
virtual void applicationWillEnterForeground();     //從待機狀態恢復時調用};

#endif // _APP_DELEGATE_H_    //結束定義

//AppDelegate.h 分析-------








AppDelegate.cpp

//AppDelegate.cpp 分析-------

 #include "AppDelegate.h"
#include "HelloWorldScene.h"

USING_NS_CC;
AppDelegate::AppDelegate() { }
AppDelegate::~AppDelegate() { }
bool AppDelegate::applicationDidFinishLaunching() {

    //初始化導演
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::create("My Game");
        director->setOpenGLView(glview);
    }

    director->setDisplayStats(true);   //開啟顯示 FPS
    director->setAnimationInterval(1.0 / 60);   //設定FPS. 預設為 1.0/60 每秒60格

    auto scene = HelloWorld::createScene();  //建立 HelloWorld場景. 它是 autorelease 物件

    director->runWithScene(scene);     //執行場景

    return true;
}


void 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 again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}



//AppDelegate.cpp 分析-------








主要類別 

CCNode 為主要物件類別,又包含了

CCDirector (導演) : 一個遊戲只能有一個導演,控制整個遊戲流程
CCScene (場景) : 為主要的大分類,如選單場景,遊戲內容場景,可包含多個圖層及角色
CCLayer (圖層) : 遊戲場景可分很多層,如背景層,角色層,NPC層,物件層,UI層
CCSprite (角色) : 可以是主角,電腦角色,物件
CCMenu (選單) : 遊戲內的介面選單,可以用cocos studio 輔助建置








prettyPrint();