CCSprite(2)(cocos2dx-2.2.5)

在CCSprite(1)中主要了解了一下它的一些继承的类,这里了解一下CCSprite这个类都做了些什么(因理解有限所能做出的解释比较少,留待能力提升后丰富完善)

1>了解一下继承关系

class CC_DLL CCSprite : public CCNodeRGBA, public CCTextureProtocol


2>了解对应的成员函数

/*** Creates an empty sprite without texture. You can call setTexture method subsequently.

*

*@return An empty sprite object that is marked as autoreleased.*/

1) static CCSprite* create();

CCSprite* CCSprite::create()

{//我们可以看出精灵的create函数,只是创建了一个空的精灵对象并且调用了autorelease

CCSprite* pSprite = new CCSprite();

if (pSprite && pSprite->init())

{

pSprite->autorelease();

return pSprite;

}

CC_SAFE_DELETE(pSprite);

return NULL;

}


bool CCSprite::init(void)

{

return initWithTexture(NULL, CCRectZero);

}


/**

Create a sprite with an image filename

After creation, the rect of sprite will be the size of the image, and the offset will be (0,0).

@param pszFileName The string which indicates a path to image file, e.g.,”scene1/monster.png”

return A valid sprite object that is marked as autoreleased

*/

2) static CCSprite* create(const char* pszFileName);    //注意Const用法,只能在函数中使用不能改变

 

CCSprite* CCSprite:create(const char* pszFileName)

{//可以看出来跟上面函数的区别在于调用的初始化的函数不同

CCSprite* pobSprite = new CCSprite();

if (pobSprite && pobSprite->initWithFile(pszFileName))

{

pobSprite->autorelease();

return pobSprite;

}

CC_SAFE_DELETE(pobSprite);

return NULL;

}


bool CCSprite:initWithFile(const char* pszFileName)

{

CCAssert(pszFilename != NULL, "Invalid filename for sprite”);

CCTexture2d* pTexture = CCTextureCache::sharedTextureCache()->addImage(pszFileName);

if (pTexture)

{

CCRect rect = CCRectZero;

rect.size = pTexture->getContentSize();

return initWithTexture(pTexture, rect);

}

return false;

}


/**

Creates a sprite with an image filename and a rect.

@param pszFileName The string which indicates a path to image file, e.g.,”scene1/monster.png”

@param rect Only the contents inside rect of pszFileName's texture will be applied for this sprite.

return A valid sprite object that is marked as autoreleased

*/


3) static CCSprite* create(const char* pszFileName, const CCRect& rect)


CCSprite* CCSprite::create(const char* pszFileName, const CCRect& rect)

{//同上initWithFile中调用initWithTexture

CCSprite *pobSprite = new CCSprite();

if (pobSprite && pobSprite->initWithFile(pszFileName, rect))

{

pobSprite->autorelease();

return pobSprite;

}

CC_SAFE_DELETE(pobSprite);

return NULL;

}


/**

Creates a sprite with an exsiting texture contained in a CCTexture2D object

After creation, the rect will be the size of the texture, and the offset will be (0,0).

@param   pTexture    A pointer to a CCTexture2D object.

@return  A valid sprite object that is marked as autoreleased.

*/

4)     static CCSprite* createWithTexture(CCTexture2D *pTexture);

CCSprite* CCSprite::createWithTexture(CCTexture2D* pTexture)

{//同上,只不过是提前创建了CCTexture2D的一个对象

CCSprite* pobSprite = new CCSprite();

if (pobSprite && pobSprite->initWithTexture(pTexture))

{

pobSprite->autorelease();

return pobSprite;

}

CC_SAFE_DELETE(pobSprite);

return NULL;

}



/**

Creates a sprite with a texture and a rect.

After creation, the offset will be (0,0).

@param   pTexture    A pointer to an existing CCTexture2D object.

You can use a CCTexture2D object for many sprites.

@param   rect        Only the contents inside the rect of this texture will be applied for this sprite.

@return  A valid sprite object that is marked as autoreleased.

*/

5) static CCSprite* createWithTexture(CCTexture2D *pTexture, const CCRect& rect);

CCSprite* CCSprite::createWithTexture(CCTexture2D *pTexture, const CCRect& rect)

{//同上,没太大区别

   CCSprite *pobSprite = new CCSprite();

if (pobSprite && pobSprite->initWithTexture(pTexture, rect))

{

pobSprite->autorelease();

return pobSprite;

}

CC_SAFE_DELETE(pobSprite);

return NULL;

}


小结:所以我们知道,其实这几种创建Sprite都是用到了同样的东西,create() initWithTexture() autorelease() ,只不过是不同的创建方式,进行了封装。当直接给的就是CCTexture2D的时候,直接调用initWithTexture(),当没有CCTexture2D的时候,或是以空值传入,或是通过对应路径下的image来创建一个CCTexture2D的对象然后再创建精灵。


Creates a sprite with an sprite frame.

@param   pSpriteFrame    A sprite frame which involves a texture and a rect

@return  A valid sprite object that is marked as autoreleased.

6) static CCSprite* createWithSpriteFrame(CCSpriteFrame *pSpriteFrame);

CCSprite* CCSprite::createWithSpriteFrame(CCSpriteFrame *pSpriteFrame)

{//不同之处在于调用了initWithSpriteFrame(),接下来我们看一下它做了什么

CCSprite *pobSprite = new CCSprite();

   if (pSpriteFrame && pobSprite && pobSprite->initWithSpriteFrame(pSpriteFrame))

{

       pobSprite->autorelease();

       return pobSprite;

}

   CC_SAFE_DELETE(pobSprite);

return NULL;

}


bool CCSprite::initWithSpriteFrame(CCSpriteFrame* pSpriteFrame)

{

CCAssert( pSpriteFrame != NULL, “”);

bool bRet = initWithTexture(pSpriteFrame->getTexture(), pSpriteFrame->getRect());

setDisplayFrame(pSpriteFrame);

return bRet;

}


接下来我们看一下setDisplayFrame都干了些什么

void CCSprite::setDisplayFrame(CCSpriteFrame* pNewFrame)

{//其实跟initWithTexture还是很类似的

m_obUnflippedOffsetPositionFromCenter = pNewFrame->getOffset();

CCTexture2D* pNewTexture = pNewFrame->getTexture();

//update texture before updating texture rect

if (pNewTexture != m_pobTexture)

{

setTexture(pNewTexture);

}


//update rect

m_bRectRotated = pNewFrame->isRotated();

setTextureRect(pNewFrame->getRect(), m_bRectRotated, pNewFrame->getOriginalSize());

}


/**

Creates a sprite with an sprite frame name.

A CCSpriteFrame will be fetched from the CCSpriteFrameCache by pszSpriteFrameName param.

If the CCSpriteFrame doesn't exist it will raise an exception.

@param   pszSpriteFrameName A null terminated string which indicates the sprite frame name.

@return  A valid sprite object that is marked as autoreleased.

*/

7)     static CCSprite* createWithSpriteFrameName(const char *pszSpriteFrameName);


CCSprite* CCSprite:createWithSpriteFrameName(const char* pszSpriteFrameName)

{

CCSpriteFrame* pFrame = CCSpriteFrameCache()::sharedSpriteFrameCache()->spriteFrameByName(pszSpriteFrameName);

return createWithSpriteFrame(pFrame);

}


8) CCSprite::CCSprite(void) //构造函数

: m_bShouldBeHidden(false),

m_pobTexture(NULL) //CCTexture2D的一个指针

{

}


9)CCSprite::~CCSprite(void) //析构函数

{

    CC_SAFE_RELEASE(m_pobTexture);

}

接下来就是初始化的函数了

/**

Initializes an empty sprite with nothing init.

*/

10)virtual bool init(void);

bool CCSprite::init()

{

return initWithTexture(NULL, CCRectZero);

}


11)bool CCSprite::initWithTexture(CCTexture2D* pTexture, const CCRect& rect)

{

return initWithTexture(pTexture, rect, false);

}


12)bool CCSprite::initWithTexture(CCTexture2D* pTexture, const CCRect& rect, bool retated)

{

if (CCNodeRGAB::init()) //CCNodeRGBA初始化后,颜色透明度都是255,并且能否传播为false

{

m_pobBatchNode = NULL; //对象的BatchNode为空

m_bRecursiveDirty = false; //是否要递归的更新(否)

setDirty(false); //是否要更新(fou)

m_pOpacityModifyRGB = true; //透明度更新颜色(是)

m_sBlendFunc.src = CC_BLEND_SRC; //混合功能的源因子 = CC_BLEND_SRC

m_sBlendFunc.dst = CC_BLEND_DST; //混合功能的目的因子 = CC_BLEND_DST

m_bFlipX = m_bFlipY = false; //X轴和Y轴翻转为false

// default transform anchor: center

setAnchorPoint(ccp(0.5f, 0.5f));

// zwoptex default values

m_obOffsetPosition = CCPointZero;

m_bHasChildren = false;

// clean the Quad

memset(&m_sQuad, 0, sizeof(m_sQuad));

// Atlas: Color

ccColor4B tmpColor = { 255, 255, 255, 255 };

m_sQuad.bl.colors = tmpColor;

m_sQuad.br.colors = tmpColor;

m_sQuad.tl.colors = tmpColor;

m_sQuad.tr.colors = tmpColor;

// shader program

setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));

// update texture (calls updateBlendFunc)

setTexture(pTexture);

setTextureRect(rect, rotated, rect.size);

// by default use "Self Render”.

// if the sprite is added to a batchnode, then it will automatically switch to "batchnode Render”

setBatchNode(NULL);


return true;

}

else

{

return false;

}

}


13)virtual bool initWithTexture(CCTexture2D* pTexture, const CCRect& rect);

从上得知这也是个过度的函数


14)virtual bool initWithTexture(CCTexture2D* pTexture, const CCRect& rect, bool rotated);

同上


15) virtual bool initWithSpriteFrame(CCSpriteFrame* pSpriteFrame);

同上


--其他init函数跳过,前面create函数的时候,已经大体的都看过了

@name Functions inherited from CCTextureProtocol //继承自CCTextureProtocol的函数

16)virtual void setTexture(CCTexture2D* texture);

void CCSprite::setTexture(CCTexture2D* texture)

{

// If batchnode, then texture id should be the same

CCAssert(! m_pobBatchNode || texture->getName() == m_pobBatchNode->getTexture()->getName(), "CCSprite: Batched sprites should use the same texture as the batchnode");

// accept texture==nil as argument

CCAssert( !texture || dynamic_cast<CCTexture2D*>(texture), "setTexture expects a CCTexture2D. Invalid argument”);


if (NULL == texture)

{//传入的texture为空的时候,创建一个白色的texture

// Gets the texture by key firstly

texture = CCTextureCache::sharedTextureCache()->textureForKey(CC_2x2_WHITE_IMAGE_KEY);


// If texture wasn’t in cache, create it from RAW data.

if (NULL == texture)

{

CCImage* image = new CCImage();

bool isOK = image->initWithImageData(cc_2x2_white_image, sizeof(cc_2x2_white_image), CCImage::kFmtRawData, 2, 2, 8);


texture = CCTextureCache::sharedTextureCache()->addUIImage(image,CC_2x2_WHITE_IMAGE_KEY);

CC_SAFE_RELEASE(image);

}

}


if (!m_pobBatchNode && m_pobTexture != texture)

{

CC_SAFE_RETAIN(texture);

CC_SAFE_RELEASE(m_pobTexture);

m_pobTexture = texture;

updateBlendFunc();

}

}


17)virtual void CCTexture2D* getTexture(void);

CCTexture2D* CCSprite::getTexture(void)

{

return m_pobTexture;

}


18) 

inline void setBlendFunc(ccBlendFunc blendFunc) { m_sBlendFunc = blendFunc; }

inline ccBlendFunc getBlendFunc(void) { return m_sBlendFunc; }


19)

在接下来就是继承自CCNode的类函数的重写

virtual void setScaleX(float fScaleX);

virtual void setScaleY(float fScaleY);

virtual void setPosition(const CCPoint& pos);

virtual void setRotation(float fRotation);

virtual void setRotationX(float fRotationX);

virtual void setRotationY(float fRotationY);

virtual void setSkewX(float sx);

virtual void setSkewY(float sy);

virtual void removeChild(CCNode* pChild, bool bCleanup);

virtual void removeAllChildrenWithCleanup(bool bCleanup);

virtual void reorderChild(CCNode *pChild, int zOrder);

virtual void addChild(CCNode *pChild);

virtual void addChild(CCNode *pChild, int zOrder);

virtual void addChild(CCNode *pChild, int zOrder, int tag);

virtual void sortAllChildren();

virtual void setScale(float fScale);

virtual void setVertexZ(float fVertexZ);

virtual void setAnchorPoint(const CCPoint& anchor);

virtual void ignoreAnchorPointForPosition(bool value);

virtual void setVisible(bool bVisible);

virtual void draw(void);


20)

接下来继承自CCNodeRGBA的函数重写

virtual void setColor(const ccColor3B& color3);

virtual void updateDisplayedColor(const ccColor3B& parentColor);

virtual void setOpacity(GLubyte opacity);

virtual void setOpacityModifyRGB(bool modify);

virtual bool isOpacityModifyRGB(void);

virtual void updateDisplayedOpacity(GLubyte parentOpacity);


21)

接下来BatchNode methods

Updates the quad according the rotation, position, scale values

virtual void updateTransform(void);


Returns the batch node object if this sprite is rendered by CCSpriteBatchNode

@return The CCSpriteBatchNode object if this sprite is rendered by CCSpriteBatchNode,

NULL if the sprite isn't used batch node.

virtual CCSpriteBatchNode* getBatchNode(void);


Sets the batch node to sprite

@warning This method is not recommended for game developers. Sample code for using batch node

@code

CCSpriteBatchNode *batch = CCSpriteBatchNode::create("Images/grossini_dance_atlas.png", 15);

CCSprite *sprite = CCSprite::createWithTexture(batch->getTexture(), CCRectMake(0, 0, 57, 57));

batch->addChild(sprite);

layer->addChild(batch);

@endcode

virtual void setBatchNode(CCSpriteBatchNode *pobSpriteBatchNode);

@end of BatchNode methods


22)

Updates the texture rect of the CCSprite in points.

It will call setTextureRect:rotated:untrimmedSize with rotated = NO, and utrimmedSize = rect.size.

virtual void setTextureRect(const CCRect& rect);


Sets the texture rect, rectRotated and untrimmed size of the CCSprite in points.

It will update the texture coordinates and the vertex rectangle.

virtual void setTextureRect(const CCRect& rect, bool rotated, const CCSize& untrimmedSize);

Sets the vertex rect.

It will be called internally by setTextureRect.

Useful if you want to create 2x images from SD images in Retina Display.

Do not call it manually. Use setTextureRect instead.

virtual void setVertexRect(const CCRect& rect);

@end of texture methods


23)

@name Frames methods

Sets a new display frame to the CCSprite.

virtual void setDisplayFrame(CCSpriteFrame *pNewFrame);

Returns whether or not a CCSpriteFrame is being displayed

virtual bool isFrameDisplayed(CCSpriteFrame *pFrame);

Returns the current displayed frame.

virtual CCSpriteFrame* displayFrame(void);

@End of frames methods


24)

@name Animation methods

Changes the display frame with animation name and index.

The animation name will be get from the CCAnimationCache

virtual void setDisplayFrameWithAnimationName(const char *animationName, int frameIndex);

@end of animation methods 


25)

@name Sprite Properties' setter/getters


Whether or not the Sprite needs to be updated in the Atlas.

@return true if the sprite needs to be updated in the Atlas, false otherwise.

inline virtual bool isDirty(void) { return m_bDirty; }

Makes the Sprite to be updated in the Atlas.

inline virtual void setDirty(bool bDirty) { m_bDirty = bDirty; }

Returns the quad (tex coords, vertex coords and color) information.

inline ccV3F_C4B_T2F_Quad getQuad(void) { return m_sQuad; }

Returns whether or not the texture rectangle is rotated.

inline bool isTextureRectRotated(void) { return m_bRectRotated; }

Returns the index used on the TextureAtlas.

inline unsigned int getAtlasIndex(void) { return m_uAtlasIndex; }

Sets the index used on the TextureAtlas.

@warning Don't modify this value unless you know what you are doing

inline void setAtlasIndex(unsigned int uAtlasIndex) { m_uAtlasIndex = uAtlasIndex; }

Returns the rect of the CCSprite in points

inline const CCRect& getTextureRect(void) { return m_obRect; }

Gets the weak reference of the CCTextureAtlas when the sprite is rendered using via CCSpriteBatchNode

inline CCTextureAtlas* getTextureAtlas(void) { return m_pobTextureAtlas; }

Sets the weak reference of the CCTextureAtlas when the sprite is rendered using via CCSpriteBatchNode

inline void setTextureAtlas(CCTextureAtlas *pobTextureAtlas) { m_pobTextureAtlas = pobTextureAtlas; }

Gets the offset position of the sprite. Calculated automatically by editors like Zwoptex.

inline const CCPoint& getOffsetPosition(void) { return m_obOffsetPosition; }


bool isFlipX(void);

Sets whether the sprite should be flipped horizontally or not.

@param bFlipX true if the sprite should be flipped horizaontally, false otherwise.

void setFlipX(bool bFlipX);

bool isFlipY(void);

void setFlipY(bool bFlipY);

@end of Sprite properties getter/setters


26)

protected:

void updateColor(void);

virtual void updateBlendFunc(void);

virtual void setReorderChildDirtyRecursively(void);

virtual void setDirtyRecursively(bool bValue);

@end of functions


接下来是成员变量

// Data used when the sprite is rendered using a CCSpriteSheet

CCTextureAtlas* m_pobTextureAtlas; // CCSpriteBatchNode Texture

unsigned int        m_uAtlasIndex; // Absolute (real) Index on the SpriteSheet

CCSpriteBatchNode* m_pobBatchNode; // Used batch node(weak reference)

bool m_bDirty; // Whether the sprite needs to be updated

bool m_bRecursiveDirty; // Whether all of the sprite’s children needs to be updated

bool m_bHasChildren; // Whether the sprite contains children

bool m_bShouldBeHidden; // should not be drawn because of the ancestor is not visible

CCAffineTransform m_transformToBatch;


//Data used when the sprite is self-rendered

ccBlendFunc m_sBlendFunc; // It’s required for CCTextureProtocol inheritance

CCTexture2D* m_pobTexture; // CCTexture2D object that is used to render the sprite



// Shared data


// texture

CCRect m_obRect; // Retangle of CCTexture2D

bool m_bRectRotated; // Whether the texture is rotated


// Offset Position (used by Zwoptex)

CCPoint m_obOffsetPosition;

CCPoint m_obUnflippedOffsetPositionFromCenter;


// vertex coords, texture coords and color info

ccV3F_C4B_T2F_Quad m_sQuad;


// opacity and RGB protocol

bool m_bOpacityModifyRGB;


// image is flipped

bool m_bFlipX;  // Whether the sprite is flipped horizontally or not

bool m_bFlipY; // Whether the sprite is flipped vertically or not


CCSprite end 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值