您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x 类大全及其概要

2013-10-30 11:17 561 查看
转自 http://blog.csdn.net/dingxiaowei2013/article/details/11014711
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的作用时,一直被一段代码困惑,后来弄明白了,这里说一下。

[cpp] view
plaincopy

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的坐标加入了原坐标。

[cpp] view
plaincopy

if (m_bIgnoreAnchorPointForPosition)

{

x += m_tAnchorPointInPoints.x;

y += m_tAnchorPointInPoints.y;

}

后来才明白,这是为了补偿后面旋转带来的偏差的。

[cpp] view
plaincopy

// 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的时候如果已经有动画播放,那么新动画和旧动画即将一起播放)

[cpp] view
plaincopy

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值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: