cocos2dx-2.x CCString代码

前端之家收集整理的这篇文章主要介绍了cocos2dx-2.x CCString代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、继承自CCObject

class CC_DLL CCString : public CCObject

2、如果通过create创建,会加入自动释放池,通过new创建则不会,所以使用时要记得retain,然后释放时
使用release。

CCString* CCString::create(const std::string& str)
{
    CCString* pRet = new CCString(str);
    pRet->autorelease();
    return pRet;
}
-->>
CCString::CCString(const std::string& str)
    :m_sString(str)
{}
3、通过字节数组
CCString* CCString::createWithData(const unsigned char* pData,unsigned long nLen)
{
    CCString* pRet = NULL;
    if (pData != NULL)
    {
        char* pStr = (char*)malloc(nLen+1);
        if (pStr != NULL)
        {
            pStr[nLen] = '\0';
            if (nLen > 0)
            {
                memcpy(pStr,pData,nLen);
            }
            
            pRet = CCString::create(pStr);
            free(pStr);
        }
    }
    return pRet;
}

4、格式化创建
CCString::createWithFormat("%s%02d.png",c,1);
CCString* CCString::createWithFormat(const char* format,...)
{
    CCString* pRet = CCString::create("");
    va_list ap;
    va_start(ap,format);
    pRet->initWithFormatandValist(format,ap);
    va_end(ap);

    return pRet;
}

5、读取文件创建
CCString* CCString::createWithContentsOfFile(const char* pszFileName)
{
    unsigned long size = 0;
    unsigned char* pData = 0;
    CCString* pRet = NULL;
    pData = CCFileUtils::sharedFileUtils()->getFileData(pszFileName,"rb",&size);
    pRet = CCString::createWithData(pData,size);
    CC_SAFE_DELETE_ARRAY(pData);
    return pRet;
}

6、获取C类型的字符
const char* CCString::getCString() const
{
    return m_sString.c_str();
}

7、获取长度
unsigned int CCString::length() const
{
    return m_sString.length();
}

8、相等比较
bool CCString::isEqual(const CCObject* pObject)
{
    bool bRet = false;
    const CCString* pStr = dynamic_cast<const CCString*>(pObject);
    if (pStr != NULL)
    {
        if (0 == m_sString.compare(pStr->m_sString))
        {
            bRet = true;
        }
    }
    return bRet;
}

9、析构
CCString::~CCString()
{ 
    m_sString.clear();
}

猜你在找的Cocos2d-x相关文章