CCNode
節點類是Cocos2D-x中的主要類,繼承自CCObject。
任何需要畫在螢幕上的對象都是節點類。最常用的節點類包括場景類(CCScene)、布景層類(CCLayer)、人物精靈類(CCSprite)、菜單類(CCMenu)
CCNode類包含的主要功能如下:
每個節點都可以包含有子節點。
節點含有周期性的毀掉方法(Schedule、Unschedule等)。
可以含有動作(CCAction)。
CCDirector
CCDirector類是Cocos2D-x遊戲引擎的核心,用來建立并且控制着主螢幕的顯示,同時控制場景的顯示時間和方式。在整個遊戲裡一般隻有一個導演。遊戲開始、結束、暫停都會調用CCDirector類的方法。
CCDirector類具有如下功能:
1.初始化OpenGL回話。
2.設定OpenGL的一些參數和方式。
3.通路和改變場景以及通路Cocos2D-x的配置細節。
4.通路視圖。
5.設定投影和朝向
需要說明一下,CCDirector是單例模式,調用CCDirector的方法标準:CCDirector::sharedDirector()->函數名
CCCamera
CCCamera類可以實作節點對象的縮放和旋轉等。
CCTouchDispatcher
1.注冊的代理以優先級排序,在addTargetedDelegate()時完成插入,delegate的優先級通過在隊列的位置來展現,優先級别高的位置靠前(雖然可以指定優先級數值,但内部沒有任何優先級記錄),相同優先級的delegates,後插入的位置靠前。
CCCardinalSplineBy
1.這個類是樣條曲線動作,其建立函數CCCardinalSplineBy::create(float duration, cocos2d::CCPointArray *points, float tension);中duration是時間間隔,points是控制點清單,tension是松緊程度。tension==1時,樣條線是分段直線。tension<1向外松弛彎曲,tension>1向内縮緊彎曲。By動作是以目前坐标為新坐标原點。
CCLayer,CCScene
這兩個類最特殊的一點是m_bIgnoreAnchorPoint(2.0.4版本是這名變量名,之前的好像是m_bRelativeToAnchorPoint),其作用是表明在布置CCLayer和CCScene對象時,是否基于AnchorPoint。CCLayer和CCScene中這兩個變量都是true(2.0.4的CCNode構造函數中的注釋寫錯了,它居然說CCLayer,CCScene應該設定這個為true)。但即使m_bIgnoreAnchorPoint為true,AnchorPoint在旋轉中起到軸心點作用并沒有變,是以在CCLayer構造函數中調用了setAnchorPoint( 0.5, 0.5 )來保證中心旋轉點。另外我之前在追究m_bIgnoreAnchorPoint的作用時,一直被一段代碼困惑,後來弄明白了,這裡說一下。
CCAffineTransform CCNode::nodeToParentTransform(void)
{
if (m_bIsTransformDirty)
{
// Translate values
float x = m_tPosition.x;
float y = m_tPosition.y;
if (m_bIgnoreAnchorPointForPosition)
{
x += m_tAnchorPointInPoints.x;
y += m_tAnchorPointInPoints.y;
}
// Rotation values
float c = 1, s = 0;
if (m_fRotation)
float radians = -CC_DEGREES_TO_RADIANS(m_fRotation);
c = cosf(radians);
s = sinf(radians);
bool needsSkewMatrix = ( m_fSkewX || m_fSkewY );
// optimization:
// inline anchor point calculation if skew is not needed
if (! needsSkewMatrix && !m_tAnchorPointInPoints.equals(CCPointZero))
x += c * -m_tAnchorPointInPoints.x * m_fScaleX + -s * -m_tAnchorPointInPoints.y * m_fScaleY;
y += s * -m_tAnchorPointInPoints.x * m_fScaleX + c * -m_tAnchorPointInPoints.y * m_fScaleY;
// Build Transform Matrix
m_tTransform = CCAffineTransformMake( c * m_fScaleX, s * m_fScaleX,
-s * m_fScaleY, c * m_fScaleY,
x, y );
// XXX: Try to inline skew
// If skew is needed, apply skew and then anchor point
if (needsSkewMatrix)
CCAffineTransform skewMatrix = CCAffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(m_fSkewY)),
tanf(CC_DEGREES_TO_RADIANS(m_fSkewX)), 1.0f,
0.0f, 0.0f );
m_tTransform = CCAffineTransformConcat(skewMatrix, m_tTransform);
// adjust anchor point
if (!m_tAnchorPointInPoints.equals(CCPointZero))
{
m_tTransform = CCAffineTransformTranslate(m_tTransform, -m_tAnchorPointInPoints.x, -m_tAnchorPointInPoints.y);
}
m_bIsTransformDirty = false;
}
return m_tTransform;
}
上述代碼中我一直不明白為什麼m_bIgnoreAnchorPoint是true的時候,将m_tAnchorPointInPoints的坐标加入了原坐标。
if (m_bIgnoreAnchorPointForPosition)
x += m_tAnchorPointInPoints.x;
y += m_tAnchorPointInPoints.y;
後來才明白,這是為了補償後面旋轉帶來的偏差的。
// optimization:
// inline anchor point calculation if skew is not needed
if (! needsSkewMatrix && !m_tAnchorPointInPoints.equals(CCPointZero))
x += c * -m_tAnchorPointInPoints.x * m_fScaleX + -s * -m_tAnchorPointInPoints.y * m_fScaleY;
y += s * -m_tAnchorPointInPoints.x * m_fScaleX + c * -m_tAnchorPointInPoints.y * m_fScaleY;
CCAction這個類是動作的基類,有點需要注意的就是,我們不光可以通過CCSpawn讓動畫一起播放,我們在調用runAction的時候本身就是一種一起播放(即在調用runAction的時候如果已經有動畫播放,那麼新動畫和舊動畫即将一起播放)
CCMotionStreak(拖動漸隐效果類)
這個類是個運動殘影功能,拖一個影子在背後。
static CCMotionStreak* create(float fade, float minSeg, float stroke, ccColor3B color, const char* path);
fade:殘影殘存時間。
misSeg:測試了一會發現沒太多感覺。一般設定為3就可以了。
stroke:殘影的寬度。
color:将會添加再殘影紋理上的顔色。
stroke:是其中的path是紋理路徑,
這個紋理将成為殘影,color将會和紋理疊加。值得注意的是,這個類重載了setPosition并使用另外一個坐标變量,是以執行一些位置類運動會詭異的現象,如CCMoveBy,因為這些運動會通過原來的坐标變量來擷取目标對象的起始坐标,但原來坐标已經被廢棄。
CCAnimationCache
這個類相當于簡單的動畫管理器,我們将動畫加進去之後,以後可以友善的去取。這個函數加載動畫的函數中有個比較好的函數:
void addAnimationsWithFile(const char* plist);
讀取一個屬性清單檔案,然後根據裡面列出的所有動畫名稱及其相關的序列幀就可以加載多個動畫,前提是這些動畫的序列幀已經存在于SpriteFrameCache中。
CCTouch
這類中是對目前使用者觸摸點的封裝,但更值得慶幸的是,在一次觸摸消息流程中,你能通過這個類獲得上一次坐标點,比如使用者觸摸螢幕,并滑動,最後松開。在這個過程中,你始終能通過getPreviousLocation()獲得上一個坐标點。
CCRenderTexture
這個類是個渲染目标體,我們可以通過begin,end函數組織一次繪畫。在begin(),end()之間使用節點的visit()函數,即可将元素畫到渲染目标體上。這裡有一點很重要,所有的渲染預設情況下都是會開啟顔色混合的。預設的是GL_ONE, GL_ONE_MINUS_SRC_ALPHA。顔色混合公式中也會作用于alpha值。
本文轉蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366556,如需轉載請自行聯系原作者