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

CCSprite(1)(cocos2dx-2.2.5)

2015-09-15 17:32 447 查看
自己写代码的经验告诉自己,注释往往是为了帮助理解的,所以先看一下对于CCSprite的注释.

/**
* CCSprite is a 2d image ( http://en.wikipedia.org/wiki/Sprite_(computer_graphics) )
CCSprite是一个2d的图片/图像
* CCSprite can be created with an image, or with a sub-rectangle of an image.
CCSprite可以用一个图片,或者是图片的一个子矩形来创建

精灵的父类是CCSpriteBatchNode和普通CCNode的区别如下
1> If the parent or any of its ancestors is a CCSpriteBatchNode then the following features/limitations are valid
如果它的任一父类是一个CCSpriteBatchNode那么下面的这些特性都是可靠的
* - Features when the parent is a CCBatchNode:
父类是CCBatchNode时的特性
* - MUCH faster rendering, specially if the CCSpriteBatchNode has many children. All the children will be drawn in a single batch.
渲染快很多,尤其是CCSpriteBatchNode有许多children时,所有的子节点将会一次绘制出来
* - Limitations (限制)
* - Camera is not supported yet (eg: CCOrbitCamera action doesn't work)
相机还不支持(例如:CCOrbitCamera action无效)
* - GridBase actions are not supported (eg: CCLens, CCRipple, CCTwirl)
GridBase 动作不支持(例如:CCLens, CCRipple, CCTwirl)

* - The Alias/Antialias property belongs to CCSpriteBatchNode, so you can't individually set the aliased property.
别名/反别名 特性 属于CCSpriteBatchNode, 所以无法个体的(单个的)设置别名特性
* - The Blending function property belongs to CCSpriteBatchNode, so you can't individually set the blending function property.
混合函数特性是CCSpriteBatchNode的特性,所以无法单个的设置混合函数特性
* - Parallax scroller is not supported, but can be simulated with a "proxy" sprite.
视差滚动不支持,但是可以用一个代理精灵来模拟

2> If the parent is an standard CCNode, then CCSprite behaves like any other CCNode:
如果父类是一个标准的CCNode,那么精灵拥有其他任何CCNode的特性
* - It supports blending functions
支持混合函数
* - It supports aliasing / antialiasing
支持别名/反别名
* - But the rendering will be slower: 1 draw per children.
渲染会比较慢 每次回执一个子节点
* The default anchorPoint in CCSprite is (0.5, 0.5).
CCSprite默认的锚点( 0.5, 0.5 )
*/

// 到这里,就把注释部分看完了,接下来我们看类的定义

class CC_DLL CCSprite :
public CCNodeRGBA,
public CCTextureProtocol
可以看出,CCSprite分别继承了CCNodeRGBA和CCTextureProtocol两个类,CCTextureProtocol这个类内容相对少一些,所以先从这个类入手,来看一下都做了什么

// CCTextureProtocol begin
同样的,我们还是先来看注释

/**
* CCNode objects that uses a CCTexture2D to render the images.
节点对象用一个CCTexture2D来渲染图像
* The texture can have a blending function.
纹理对应的有混合函数
* If the texture has alpha premultiplied the default blending function is:
如果纹理的alpha预乘了,默认的混合函数是:
* src=GL_ONE dst= GL_ONE_MINUS_SRC_ALPHA
src = GL_ONE dst = GL_ONE_MINUS_SRC_ALPHA
* else
否则
* src=GL_SRC_ALPHA dst= GL_ONE_MINUS_SRC_ALPHA
src = GL_SRC_ALPHA dst = GL_ONE_NIMUS_SRC_ALPHA
* But you can change the blending function at any time.
但是你可以在任意时间改变混合函数
* @js NA
*/
接下来我们看CCTextureProtocol类

class CC_DLL CCTextureProtocol :
public CCBlendProtocol
CCTextureProtocol类又继承了CCBlendProtocol类,为了便于我们理解,所以还是刨根问底吧,继续跟进,看一下CCBlendProtocol类

// CCBlendProtocol begin
还是老规矩,先看注释

/**
* Specify the blending function according glBlendFunc
依照glBlendFunc指定混合函数
* Please refer to glBlendFunc in OpenGL ES Manual
有关glBlendFunc请参照 OpenGL ES Manual (OpenGL ES 手册)
* http://www.khronos.org/opengles/sdk/docs/man/xhtml/glBlendFunc.xml for more details.
* @js NA
* @lua NA
*/
接下来,我们来看CCBlendProtocol类

class CC_DLL CCBlendProtocol // 关于CC_DLL http://www.ziliao1.com/Article/Show/716662CB4AD94EB2D020C3E74EA62A4C.html有详解 终于没有父类了,那么接下来,我们看这个类的成员函数
老规矩,结合注释来看

/**
* Sets the source blending function.
设置资源混合函数
* @param blendFunc A structure with source and destination factor to specify pixel arithmetic,
* e.g. {GL_ONE, GL_ONE}, {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}.
参数 blendFunc 是一个包含源因子和目的因子的像素运算的结构体 例如 { GL_ONE, GL_ONE}, { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}
*/
virtual
void setBlendFunc(ccBlendFunc blendFunc) =
0;

* Returns the blending function that is currently being used.
返回当前使用的混合功能(同前的混合函数,因第一次接触,所以有些地方可能翻译不是特别合适)
* @return A ccBlendFunc structure with source and destination factor which specified pixel arithmetic.
返回一个ccBlendFunc结构体包含指定了像素运算的源因子和目的因子
*/
virtual
ccBlendFunc getBlendFunc(void) =
0;
// CCBlendProtocol end
到这里,CCBlendProtocol我们就看完了,接下来,返回去我们接着来看CCTextureProtocol
我们已经看过了CCTextureProtocol的继承关系,知道它继承的 CCBlendProtocol类,接下来,我们来看一下CCTextureProtocol的成员函数
1>
/**
* Returns the currently used texture
返回当前使用的texture
* @return The texture that is currenlty being used.
* @lua NA
*/
virtual
CCTexture2D* getTexture(void) =
0;
2>

/**
* Sets a new texuture. It will be retained.
设置一个新的texture. 会被retained
* @param texture A valid CCTexture2D object, which will be applied to this sprite object.
参数 texture 是一个可使用的CCTexture2D对象,将被应用到这个精灵对象
* @lua NA
*/
virtual
void setTexture(CCTexture2D *texture) =
0;
CCTextureProtocol end (注意一点这两个protocol类都是纯虚类,CCTextureProtocol继承了CCBlendProtocol后,并没有在类中重写它的函数,那应该是只是单纯的继承下来了两个纯虚函数,这种用法,自己还是第一次见到,以后可以看一下C++上有没有相关的说法)
到这里, CCTextureProtocol我们也看完了,接下来,我们来查看精灵继承的另一个父类也就是CCNodeRGBA

// CCSprite的第二个父类CCNodeRGBA
CCNodeRGBA begin

class CC_DLL CCNodeRGBA :
public CCNode,
public CCRGBAProtocol
很明显CCNodeRGBA又有两个父类,从前面来看,我们认为protocol应该是方法的类,结构应该会简单一些,所以我们先来查看这个类

CCRGBAProtocol begin

/**
* RGBA protocol that affects CCNode's color and opacity
RGBA协议决定了CCNo的颜色和透明度
* @js NA
*/
class CC_DLL CCRGBAProtocol
可以看到CCRGBAProtocol没有父类了,那么接下来我们来查看它的成员函数
1>

/**
* Changes the color with R,G,B bytes
用R, G, B 字节改变颜色
* @param color Example: ccc3(255,100,0) means R=255, G=100, B=0
参数color 例: ccc3(255, 100, 0) 指的是 R = 255, G = 100, B = 0
*/
virtual
void setColor(const
ccColor3B& color) = 0;
2>

/**
* Returns color that is currently used.
返回当前正在使用的颜色
* @return The ccColor3B contains R,G,B bytes.
返回包含R,G,B字节的 ccColor3B (定义的一个结构体类型)
*/
virtual
const ccColor3B& getColor(void) =
0;
3>

/**
* Returns the displayed color.
返回显示的颜色
* @return The ccColor3B contains R,G,B bytes.
返回包含R,G,B字节的 ccColor3B (定义的一个结构体类型)

*/
virtual
const ccColor3B& getDisplayedColor(void) =
0;
4>

/**
* Returns the displayed opacity.
返回显示的透明度
* @return The opacity of sprite, from 0 ~ 255
返回sprite的透明度,范围0-255
*/
virtual
GLubyte getDisplayedOpacity(void) =
0;
5>

/**
* Returns the opacity.
返回透明度
* The opacity which indicates how transparent or opaque this node is.
* 0 indicates fully transparent and 255 is fully opaque.
透明度表明这个node多透明或者不透明
0 表示透明度满 255 表示不透明度满
* @return The opacity of sprite, from 0 ~ 255
*/
virtual
GLubyte getOpacity(void) =
0;
6>

/**
* Changes the opacity.
改变透明度
* @param value Goes from 0 to 255, where 255 means fully opaque and 0 means fully transparent.
参数 value 从0到255,255表示不透明度满 0表示透明度满
*/
virtual
void setOpacity(GLubyte opacity) =
0;

// optional
7>

/**
* Changes the OpacityModifyRGB property.
改变OpacityModifyRGB属性
* If thie property is set to true, then the rendered color will be affected by opacity.
如果属性被设置为true,那么渲染的颜色会被透明度影响
* Normally, r = r * opacity/255, g = g * opacity/255, b = b * opacity/255.
通常, r = r * opacity/255, g = g * opacity/255, b = b * opacity/255
* @param bValue true then the opacity will be applied as: glColor(R,G,B,opacity);
参数 bValue 为真那么透明度将以glColor(R,G,B,opacity)应用
* false then the opacity will be applied as: glColor(opacity, opacity, opacity, opacity);
为假,那么透明度将以goColor(opacity,opacity,opacity,opacity)应用
*/
virtual
void setOpacityModifyRGB(bool bValue) =
0;
8>

/**
* Returns whether or not the opacity will be applied using glColor(R,G,B,opacity)
返回透明度是否以glColor(R,G,B,opacity)应用
* or glColor(opacity, opacity, opacity, opacity)
或者 glColor(opacity, opacity, opacity, opacity)应用
* @return Returns opacity modify flag.
返回透明度修改标识
*/
virtual
bool isOpacityModifyRGB(void) =
0;
9>

/**
* whether or not color should be propagated to its children.
是否颜色应该传播给它的子节点
*/
virtual
bool isCascadeColorEnabled(void) =
0;
virtual
void setCascadeColorEnabled(bool cascadeColorEnabled) =
0;
10>

/**
* recursive method that updates display color
递归方法更新显示颜色
*/
virtual
void updateDisplayedColor(const
ccColor3B& color) = 0;
11>

/**
* whether or not opacity should be propagated to its children.
透明度是否传播给它的子节点
*/
virtual
bool isCascadeOpacityEnabled(void) =
0;
virtual
void setCascadeOpacityEnabled(bool cascadeOpacityEnabled) =
0;
12>

/**
* recursive method that updates the displayed opacity.
递归方法更新显示的透明度
*/
virtual
void updateDisplayedOpacity(GLubyte opacity) =
0;
CCRGBAProtocol end
到这里,CCRGBAProtocol就结束了然后我们回到CCNodeRGBA,接着来查看它的另一个父类
CCNode begin

/** @brief CCNode is the main element. Anything that gets drawn or contains things that get drawn is a CCNode.
The most popular CCNodes are: CCScene, CCLayer, CCSprite, CCMenu.
简介 CCNode是主要的元素.任何绘制的或者是包含能被绘制的东西的都是一个CCNode
比较特殊的CCNodes有: CCScene, CCLayer, CCSprite, CCMenu
The main features of a CCNode are:
CCNode的主要特性有:
- They can contain other CCNode nodes (addChild, getChildByTag, removeChild, etc)
他们可以包含其他的CCNode 节点 (addChild, getChildByTag, removeChild, 等)
- They can schedule periodic callback (schedule, unschedule, etc)
他们可以设定一个定期的回调 ( schedule, unschedule, 等)
- They can execute actions (runAction, stopAction, etc)
他们可以执行动作(runAction, stopAction,等)
Some CCNode nodes provide extra functionality for them or their children.
一些CCNode节点提供额外的功能给他们或他们的子节点
Subclassing a CCNode usually means (one/all) of:
子类化一个CCNode通常意味着 (一个/全部)
- overriding init to initialize resources and schedule callbacks
重载init到初始化资源和定时回调
- create callbacks to handle the advancement of time
创建回调处理时间推进
- overriding draw to render the node
重载draw来渲染node

Features of CCNode:
CCNode特性:
- position -位置
- scale (x, y)
-缩放(x,y)
- rotation (in degrees, clockwise)
-旋转(度数,顺时针)
- CCCamera (an interface to gluLookAt )
-CCCamera(一个到gluLookAt的接口)
- CCGridBase (to do mesh transformations)
-CCGridBase(做网孔转换)
- anchor point
-锚点
- size -尺寸
- visible -可视化
- z-order -z坐标
- openGL z position
-openGL Z轴位置

Default values:
- 默认值:
- rotation: 0
-旋转: 0
- position: (x=0,y=0)
-位置:(x=0, y=0)
- scale: (x=1,y=1)
-缩放(x=1, y=1)
- contentSize: (x=0,y=0)
内容尺寸(x=0, y=0)
- anchorPoint: (x=0,y=0)
锚点(x=0, y=0)

Limitations:
局限:
- A CCNode is a "void" object. It doesn't have a texture
一个CCNode是一个"空"对象, 它不包含有texture

Order in transformations with grid disabled
grid disabled(转换顺序)
-# The node will be translated (position)
-# 节点会被转化(位置)
-# The node will be rotated (rotation)
-# 节点会被旋转(旋转)
-# The node will be scaled (scale)
-# 节点会被缩放(缩放)
-# The node will be moved according to the camera values (camera)
-# 节点会被移动依照camera值(camera)

Order in transformations with grid enabled
grid enabled(转换顺序)
-# The node will be translated (position)
-# 节点会被转化(位置)
-# The node will be rotated (rotation)
-# 节点会被旋转(旋转)
-# The node will be scaled (scale)
-# 节点会被缩放(缩放)
-# The grid will capture the screen
-# grid(网格)会捕获屏幕
-# The node will be moved according to the camera values (camera)
-# 节点会依据camera值被移动
-# The grid will render the captured screen
-# grid(网格)会渲染捕获的屏幕

Camera:
- Each node has a camera. By default it points to the center of the CCNode.
每个节点都有一个camera.默认指向CCNode的中心
*/
接下来,我们来查看CCNode类
CCNode begin

class CC_DLL CCNode :
public CCObject
可知,CCNode继承自CCObject
那么接下来我们先来认识一下CCObject
CCObject begin

class CC_DLL CCObject :
public CCCopying
所以,我们接下来,就先看一下CCCopying这个类
CCCopying begin

/**
* @js NA
* @lua NA
*/
class CC_DLL CCCopying
{
public:
virtual
CCObject* copyWithZone(CCZone* pZone);
};
比较简单的一个类,只有一个成员函数
接下来,我们跳转到对应的cpp文件中,来看一下这个函数到底是怎么实现的,做了些什么
CCObject* CCCopying::copyWithZone(CCZone* pZone)
{
CC_UNUSED_PARAM(pZone);
// #define CC_UNUSED_PARAM(unusedparam)
(void)unusedparam
CCAssert(0, "not implement");
// 如果一个函数有返回值,而没有被使用的话会报错,这里进行了转化不让返回
return 0;
}
@param pZone 是一个CCZone类型的,那么我们跳转到CCZone了解一下
CCZone begin

class CC_DLL CCZone
没有父类,那么我们接下来就查看成员函数

CCZone(CCObject *pObject =
NULL);
跳转到cpp对应的函数
CCZone::CCZone(CCObject* pObject)
{
m_pCopyObject = pObject;
}
成员函数只有这么一个拷贝构造函数,接下来我们来看一下成员变量

CCObject *m_pCopyObject;
CCZone end
回到CCCopying,还有一个点
CCAssert(0, "not implement");
跳转查看

#ifndef CCAssert
//如果没有定义 CCAssert
#if COCOS2D_DEBUG > 0
// 如果 COCOS2D_DEBUG > 0
extern bool
CC_DLL cc_assert_script_compatible(const
char *msg);

#define CCAssert(cond, msg) do { \
if (!(cond)) { \
if (!cc_assert_script_compatible(msg) && strlen(msg)) \
cocos2d::CCLog("Assert failed: %s", msg); \
CC_ASSERT(cond); \
} \
} while (0)
#else
#define CCAssert(cond, msg) ((void)(cond))
#endif
#endif // CCAssert

extern bool CC_DLL cc_assert_script_compatible(const char* msg) 先跳转到cc_assert_script_compatible(cons char* msg)

bool CC_DLL cc_assert_script_compatible(const
char *msg)
//compatible 兼容
{//这里涉及到的就是Lua引擎的东西了,所以暂时不再继续了解
cocos2d::CCScriptEngineProtocol* pEngine =
cocos2d::CCScriptEngineManager::sharedManager()->getScriptEngine();
if (pEngine && pEngine->handleAssert(msg))
{
return
true;
}
return
false;
}
CCCopying end

接下来,我们就返回来看CCObject,按照一贯做法,我们应该查看它的成员函数,从CCObject.h文件中,看到先声明的成员变量,随后生成的成员函数,所以
我们遵照作者的意思,先来看一下成员变量

public:
// object id, CCScriptSupport need public m_uID
// 对象id, CCScriptSupport需要public类型m_uID
unsigned
int m_uID;
// unsigned int m_uID; m(成员) u(unsigned)
// Lua reference id
// Lua引用id
int m_nLuaID;
int m_nLuaID; n(int类型,我自己认为是number方便记忆)
protected:
// count of references
// 引用计数
unsigned
int m_uReference;
unsigned int m_uReference;
// count of autorelease
// 自动释放计数
unsigned
int m_uAutoReleaseCount;
unsigned int m_uAutoReleaseCount;
接下来,我们就来查看成员函数

1> CCObject(void);

CCObject::CCObject(void)
: m_nLuaID(0)
, m_uReference(1)
// when the object is created, the reference count of it is 1 //当对象创建成功,引用计数为1
, m_uAutoReleaseCount(0)
{
static
unsigned int uObjectCount =
0;

m_uID = ++uObjectCount;
}
这里涉及到了C++的一些知识点,所以我们先来了解一下C++的东西,毕竟学到最后,其实还是C++
还是从上往下的顺序来,第一个点就是初始化列表了也就是
CCObject::CCObject(void)
: m_nLuaID(0)
, m_uReference(1)
, m_uAutoReleaseCount(0)
关于初始化列表也是有不少的东西在里面这里就不赘述了,找了一篇文章,需要的可以参阅 http://www.cnblogs.com/graphics/archive/2010/07/04/1770900.html //初始化列表

第二个点 static unsigned int uObjectCount = 0;
(static的用法,还是不是特别的清楚,接下来,下一篇文章搞清楚static的用法)
/**
* @lua NA
*/
2> virtual ~CCObject(void);

CCObject::~CCObject(void)
//CCObject的析构函数
{
// if the object is managed, we should remove it
//如果对象被管理了,那么我们应该把它从管理池移除
// from pool manager
if (m_uAutoReleaseCount >
0)
//如果自动释放计数大于0
{
CCPoolManager::sharedPoolManager()->removeObject(this);
//管理池移除对象, CCPoolManager,CCScriptEngineManager,在后续文章中再研究
}

// if the object is referenced by Lua engine, remove it
//如果对象被Lua引擎引用,移除它
if (m_nLuaID)
//如果LuaID大于0
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptObjectByCCObject(this);
//脚本引擎管理器移除脚本对象
}
else
//否则
{
CCScriptEngineProtocol* pEngine =
CCScriptEngineManager::sharedManager()->getScriptEngine();
//LuaID = 0 时
if (pEngine !=
NULL && pEngine->getScriptType() ==
kScriptTypeJavascript)
如果脚本引擎存在并且 脚本类型是kScriptTypeJavascript
{
pEngine->removeScriptObjectByCCObject(this);
//脚本引擎移除对象
}
}
}

3> void release(void);

void CCObject::release(void)
{
CCAssert(m_uReference >
0, "reference count should greater than 0");
//assert 还没有特别懂,但是大概其的知道它是一个检查报错的东西
--m_uReference;
//引用计数-1

if (m_uReference ==
0)
如果引用计数为0
{
delete
this;
那么删除这个对象
}
}

4>void retain(void);

void CCObject::retain(void)
//
{
CCAssert(m_uReference >
0, "reference count should greater than 0");
//如果m_uReference>0不成立,打印 引用计数应该大于0
++m_uReference;
//引用计数+1
}

5>CCObject* autorelease(void);

CCObject* CCObject::autorelease(void)
{
CCPoolManager::sharedPoolManager()->addObject(this);
//管理池添加对象
return
this;
//返回这个对象
}

4>CCObject* copy(void);

CCObject* CCObject::copy()
{
return
copyWithZone(0);
//这里调用到了父类CCCopying中得函数 copyWithZone()
}
5> bool isSingleReference(void)
const;

bool CCObject::isSingleReference(void)
const //是否是单一引用
{
return
m_uReference == 1;
//如果引用计数为1则是单一引用,否则不是单一引用
}
6> unsigned int retainCount(void)
const;

unsigned int
CCObject::retainCount(void)
const //引用计数
这里注意函数后面加const的用法,const成员函数,不能修改数据成员的值,不能调用非const函数
{
return
m_uReference;
//返回引用计数
}

7> virtual bool isEqual(const
CCObject* pObject);

bool CCObject::isEqual(const
CCObject *pObject)
{
return
this == pObject;
//判断两个CCObject对象是否相等
}
8> virtual void acceptVisitor(CCDataVisitor &visitor);

void CCObject::acceptVisitor(CCDataVisitor &visitor)
//接受观察者
{
visitor.visitObject(this);
//这个暂时不知道是干什么的 hlq
}

9> virtual
void update(float dt) {CC_UNUSED_PARAM(dt);};
到这里CCObject的成员函数,就基本上了解完了,最后这里我们看到类结束的地方,有这么一行代码
friend class CCAutoreleasePool;
// 友元类 到这里发现C++的知识点太多了,有必要系统的整理一下,随后将自己在学习cocos框架的过程中遇到的点整理一下
CCObject end
现在看了CCNode类,就基本把这些父类看完了,CCNode类是比较大的一个类,里面包含了很多东西,这里暂时不再看了,随后单独写一篇有关CCNode的文章
CCNode end
接下来,就回到了CCNodeRGBA这个类了
1> CCNodeRGBA();

CCNodeRGBA::CCNodeRGBA()
: _displayedOpacity(255)
//显示透明度(255)
, _realOpacity(255)
//实际透明度(255)
, _displayedColor(ccWHITE)
//显示颜色(白色)
, _realColor(ccWHITE)
//实际颜色(白色)
, _cascadeColorEnabled(false)
//能否传播颜色(否)
, _cascadeOpacityEnabled(false)
//能否传播透明度(否)
{}
CCNodeRGBA相比于CCNode就是多了RGB(颜色)和透明度,所以构造函数初始化了与颜色透明度相关的一些参数
2> CCNodeRGBA::~CCNodeRGBA() {} 析构函数
3> virtual
bool init();

bool CCNodeRGBA::init()
{
if (CCNode::init())
{
_displayedOpacity =
_realOpacity = 255;
//初始化透明度(255)
_displayedColor =
_realColor = ccWHITE;
//初始化颜色(白色)
_cascadeOpacityEnabled =
_cascadeColorEnabled = false;
//初始化是否传播(否)
return
true;
//初始化成功返回true
}
return
false;
//初始化失败返回false
}

/**
* Allocates and initializes a nodergba.
//分配空间初始化一个nodergba
* @return A initialized node which is marked as "autorelease".
//返回一个被标记为autorelease的节点 (自动释放的节点)
*/
4> static CCNodeRGBA * create(void);

CCNodeRGBA * CCNodeRGBA::create(void)
{
CCNodeRGBA * pRet =
new CCNodeRGBA();
//分配内存给一个新创建的CCNodeRGBA对象
if (pRet && pRet->init())
//如果对象存在并且初始化成功了
{
pRet->autorelease();
//那么调用autorelease() 加入自动释放池
}
else
//否则
{
CC_SAFE_DELETE(pRet);
//释放掉分配的内存(如果创建CCNodeRGBA失败或者初始化失败)
}
return pRet;
//返回一个标记为自动释放的CCNodeRGBA
}

5> virtual
GLubyte getOpacity();

GLubyte CCNodeRGBA::getOpacity(void)
//获取透明度
{
return
_realOpacity;
//返回实际透明度
}

6> virtual GLubyte getDisplayedOpacity();

GLubyte CCNodeRGBA::getDisplayedOpacity(void)
//获取显示透明度
{
return
_displayedOpacity;
//返回显示透明度
}

7> virtual void setOpacity(GLubyte opacity);

void CCNodeRGBA::setOpacity(GLubyte opacity)
{
_displayedOpacity =
_realOpacity = opacity;
//显示透明度=实际透明度= opacity(参数)

if (_cascadeOpacityEnabled)
//如果可以传播透明度
{ //改变子类的透明度
GLubyte parentOpacity =
255;
CCRGBAProtocol* pParent =
dynamic_cast<CCRGBAProtocol*>(m_pParent);
if (pParent && pParent->isCascadeOpacityEnabled())
{
parentOpacity = pParent->getDisplayedOpacity();
}
this->updateDisplayedOpacity(parentOpacity);
}
}

8> virtual void updateDisplayedOpacity(GLubyte parentOpacity);

void CCNodeRGBA::updateDisplayedOpacity(GLubyte parentOpacity)
{//递归的改变父类下所有子类的透明度
_displayedOpacity =
_realOpacity * parentOpacity/255.0;

if (_cascadeOpacityEnabled)
{
CCObject* pObj;
CCARRAY_FOREACH(m_pChildren, pObj)
{
CCRGBAProtocol* item =
dynamic_cast<CCRGBAProtocol*>(pObj);
if (item)
{
item->updateDisplayedOpacity(_displayedOpacity);
}
}
}
}

9> virtual bool isCascadeOpacityEnabled();

bool CCNodeRGBA::isCascadeOpacityEnabled(void)
{
return
_cascadeOpacityEnabled;
//透明度能否传播
}

10> virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled);

void CCNodeRGBA::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
{
_cascadeOpacityEnabled = cascadeOpacityEnabled;
//设置透明度能否传播
}
11> virtual const
ccColor3B& getColor(void);

const ccColor3B&
CCNodeRGBA::getColor(void)
{
return
_realColor;
//返回实际颜色
}

12> virtual const
ccColor3B& getDisplayedColor();

const ccColor3B&
CCNodeRGBA::getDisplayedColor()
{
return
_displayedColor;
//返回显示的颜色
}

13> virtual void setColor(const
ccColor3B& color);

void CCNodeRGBA::setColor(const
ccColor3B& color)
{
_displayedColor =
_realColor = color;
//设置颜色 显示颜色=实际颜色=参数color

if (_cascadeColorEnabled)
{//递归的改变子类的颜色
ccColor3B parentColor =
ccWHITE;
CCRGBAProtocol *parent =
dynamic_cast<CCRGBAProtocol*>(m_pParent);
if (parent && parent->isCascadeColorEnabled())
{
parentColor = parent->getDisplayedColor();
}

updateDisplayedColor(parentColor);
}
}

14> virtual void updateDisplayedColor(const
ccColor3B& parentColor);

void CCNodeRGBA::updateDisplayedColor(const
ccColor3B& parentColor)
{//递归的改变子类的颜色
_displayedColor.r =
_realColor.r * parentColor.r/255.0;
_displayedColor.g =
_realColor.g * parentColor.g/255.0;
_displayedColor.b =
_realColor.b * parentColor.b/255.0;

if (_cascadeColorEnabled)
{
CCObject *obj =
NULL;
CCARRAY_FOREACH(m_pChildren, obj)
{
CCRGBAProtocol *item =
dynamic_cast<CCRGBAProtocol*>(obj);
if (item)
{
item->updateDisplayedColor(_displayedColor);
}
}
}
}

15> virtual bool isCascadeColorEnabled();

bool CCNodeRGBA::isCascadeColorEnabled(void)
{
return
_cascadeColorEnabled;
//返回是否可以传播颜色
}

16> virtual void setCascadeColorEnabled(bool cascadeColorEnabled);

void CCNodeRGBA::setCascadeColorEnabled(bool cascadeColorEnabled)
{
_cascadeColorEnabled = cascadeColorEnabled;
//设置颜色是否可以传播
}

17> virtual
void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);};
virtual
bool isOpacityModifyRGB() {
return false; };
CCNodeRGBA end
到这里,看完了CCSprite所有的上层的关系.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: