#include "XSprite.h" CXSprite::CXSprite(void) { m_strPic.clear(); } CXSprite::~CXSprite() { } /********************************* *函数名称:createSprite *函数功能:创建精灵 *函数参数:const char* psFile--创建精灵用的图片 int nFrameIndex--(-1--普通图片 0-2--按钮图片帧(按钮统一为3帧)) *函数返回值:创建的精灵指针 *备注:分两种情况来创建 1.第一种:只是简单的通过图片来创建 2.第二种:通过纹理来创建,但是这里要将纹理三等分, 分别是未点击时的,点击时的,点击结束时的 *********************************/ CXSprite* CXSprite::createSprite( const char* psFile, int nFrameIndex /*= -1*/ ) { if(NULL == psFile || nFrameIndex < -1 || nFrameIndex >= BTN_FRAME_AMOUNT) { return NULL; } CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->addImage(psFile); ASSERT(pTexture); if(NULL == pTexture) { CCLOG("create: %s ---Texture Eroor", psFile); return NULL; } CXSprite* pSprite = new CXSprite(); if (NULL != pSprite) { pSprite->autorelease(); pSprite->m_strPic = psFile; bool bSuc = false; if(-1 == nFrameIndex) { bSuc = pSprite->initWithTexture(pTexture); } else { CCRect rect = CCRectZero; rect.size = pTexture->getContentSize(); rect.size.width /= BTN_FRAME_AMOUNT; rect.origin.x += nFrameIndex * rect.size.width; bSuc = pSprite->initWithTexture(pTexture, rect); } if(bSuc) { pSprite->setAnchorPoint(ccp(0, 0)); return pSprite; } } CC_SAFE_DELETE(pSprite); return NULL; } /********************************* *函数名称:create *函数功能:通过标签创建精灵 *函数参数:const char* psTag--创建精灵用的图片名称 int nFrameIndex--(-1--普通图片 0-2--按钮图片帧(按钮统一为3帧)) *函数返回值:创建的精灵指针 *备注: 这里是通过CCSpriteFrameCache来创建的 *********************************/ CXSprite* CXSprite::create( const char* psTag, int nFrameIndex /*= -1*/ ) { if(NULL == psTag || nFrameIndex < -1 || nFrameIndex >= BTN_FRAME_AMOUNT) { return NULL; } if(strlen(psTag) <= 0) { return NULL; } CCSpriteFrame* pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(psTag); ASSERT(pFrame); if(NULL == pFrame) { CCLOG("CXSprite::create: %s ---SpriteFrame Error", psTag); return NULL; } CXSprite* pSprite = new CXSprite(); if (NULL != pSprite) { pSprite->autorelease(); pSprite->m_strPic = psTag; bool bSuc = false; if(-1 == nFrameIndex) { bSuc = pSprite->initWithSpriteFrame(pFrame); } else { CCRect rect = pFrame->getRect(); rect.size.width /= BTN_FRAME_AMOUNT; rect.origin.x += nFrameIndex * rect.size.width; bSuc = pSprite->initWithTexture(pFrame->getTexture(), rect); } if(bSuc) { pSprite->setAnchorPoint(ccp(0, 0)); return pSprite; } } CC_SAFE_DELETE(pSprite); return NULL; } /********************************* *函数名称:getPicName *函数功能:获取图片名称 *函数参数:void *函数返回值:图片名称 *备注:返回的是成员变量 *********************************/ string CXSprite::getPicName() { return m_strPic; }