學習重點:HelloWorld工程,Cocos2D-X引擎架構 在vs2010中開啟 學習1中建立好的HelloWorld工程,瞭解cocos2dx的生命週期和引擎架構。 1,生命週期 學習引擎已經寫好了的生命週期,在AppDelegate.h中聲明了三個虛函數: applicationDidFinishLaunching : 實現導演(CCDirector)與情境(scene)初始化,applicationDidFinishLaunching -> 導演 -> 情境 -> 圖層(layer)->…… applicationDidEnterBackground :當工程進入背景時候調用此函數。 applicationWillEnterForeground :當工程進入前端的時候調用此函數。
2,引擎架構 1)設計思想:
導演(Director) -> 情境(Scene)-> 層(Layer)-> 精靈(Sprite)
導演(CCDirector):Class that creates and handle the main Window and manages how and when to execute the Scenes.切換情境,控制情境。導演是單例模式。
情境(CCScene ):布置背景。CCScene is a subclass of CCNode that is used only as an abstract concept. 情境是CCNode的子類,是一種抽象化的思想。CCScene也是所有nodes的父類。
圖層(CCLayer):多個圖層構成情境,可嵌套。CCLayer is a subclass of CCNode that implements the TouchEventsDelegate protocol.圖層是CCNode的子類且實現了TouchEventsDelegate協議。
精靈(CCSprite):放置在圖層上,顯示在情境中,具體的物體由精靈抽象化。CCSprite can be created with an image, or with a sub-rectangle of an image.精靈可以用一個圖片來建立,或也可以用一個子矩陣來建立。
動作(CCAction) :行為。 Base class for CCAction objects. CCAction對象的基礎類。
節點(CCNode):最主要的元素,所有元素的基礎。Anything that gets drawn or contains things that get drawn is a CCNode. The most popular CCNodes are: CCScene, CCLayer, CCSprite,CCMenu.任何包含的東西都是在CCNode的基礎之上,用的最普遍的是情境,圖層,精靈,菜單。
eg:“開心消消樂”的開始畫面,通過層次的方式把UI和按鈕分開來
2)構架方案:
節點樹,CCNode
攝像機,A CCCamera is used in every CCNode.
可以用於任何CCNode
Useful to look at the object from different views. The OpenGL gluLookAt() function is used to locate the camera.用來從不同的視角來看這個對象。OpenGL 公路LookAt函數可以用來擺放攝像機。
If the object is transformed by any of the scale, rotation or position attributes, then they will override the camera.如果這個對象被放大/縮小,旋轉,或者設定位置屬性,那麼他們會覆蓋攝像機。
小結:HelloWorld工程。
main函數作為程式入口,run函數中會調用AppDelegate::applicationDidFinishLaunching函數建立導演CCDirector::sharedDirector(),導演再運行情境pDirector->runWithScene(pScene)。
HelloWorld::scene();導演啟動並執行情境是在HelloWorldScene.cpp中實現的。情境建立是空的,所以要在情境中add圖層。
圖層中add精靈(CCSprite),精靈有(CCAction)等。
ps:c基礎 標頭檔(.h):
寫類的聲明(包括類裡面的成員和方法的聲明)、函數原型、#define常數等,但一般來說不寫出具體的實現。
:冒號在c++中的意思:
(1)表示“網域作業符”
例:聲明了一個類A,類A裡聲明了一個成員函數void f(),但沒有在類的聲明裡給出f的定義,那麼在類外定義f時,
就要寫成void A::f(),表示這個f()函數是類A的成員函數。
(2)直接用在全域函數前,表示是全域函數
例:在VC裡,你可以在調用API 函數裡,在API函數名前加::
(3)表示引用成員函數及變數,範圍成員運算子
例:System::Math::Sqrt() 相當於System.Math.Sqrt()。更詳細的 請點擊。