CCString

CCString

简介

CCString继承至CCObject,CCObjecte这个基类主要是为了自动内存管理而创建的。CCString提供一系列的接口,例如create,convert等等。

常用的方法

创建:

    /**使用std::string创建了一个字符串, 你也可以传递一个c字符串指针,因为std::string的构造函数可以访问c字符串指针

     * @返回的 CCString 指针是一个自动释放对象,

     *也就意味着你不需要调用release操作,除非你retain了.

     */

    static CCString* create(const std::string& str);

    /**使用格式化方式来创建一个字符串,这个方法和c语言里面的‘sprintf’类似,默认缓存大小是(1024*100)bytes

     *假如你想要改变这个缓存大小,你可以去CCString.cpp中,更改kMaxStringLen 这个宏定义。

     * @返回的 CCString 指针是一个自动释放对象,

     *也就意味着你不需要调用release操作,除非你retain了.

     */ 

    static CCString* createWithFormat(const char* format, …);

    /** 使用二进制数据来创建字符串 

     * @返回的 CCString 指针是一个自动释放对象,

     *也就意味着你不需要调用release操作,除非你retain了.

     */

    static CCString* createWithData(const unsigned char* pData, unsigned long nLen);

    /**使用一个文件来创建一个字符串, 

     * @return A CCString pointer which is an autorelease object pointer,

     * it means that you needn‘t do a release operation unless you retain it.

     */
    static CCString* createWithContentsOfFile(const char* pszFileName);

转换

CCString允许CCString实例变量转换为另外类型的变量。

    /** convert to int value */

    int intValue() const;

    /** convert to unsigned int value */

    unsigned int uintValue() const;

    /** convert to float value */

    float floatValue() const;

    /** convert to double value */

    double doubleValue() const;

    /** convert to bool value */

    bool boolValue() const;    

常用的宏定义

    #define CCStringMake(str) CCString::create(str)

    #define ccs CCStringMake

使用这些宏可以非常方便的构建一个自动释放的CCString对象。假如你想要新建很多的CCString对象并把他们增加到CCArray中。

使用下面的代码就可以实现了,并且这些代码看起来相当简洁。

    CCArray *stringArray = CCArray::create(

        ccs("Hello"),

        ccs("Variable"),

        ccs("Size"),

        ccs("!"),

        NULL);
时间: 2024-08-21 10:22:33

CCString的相关文章

7.数据本地化CCString,CCArray,CCDictionary,tinyxml2,写入UserDefault.xml文件,操作xml,解析xml

 数据本地化 A CCUserDefault 系统会在默认路径cocos2d-x-2.2.3\projects\Hello\proj.win32\Debug.win32下生成一个名为UserDefault.xml.所有的key皆为char *型,value类型为bool intfloat double std::string. 读操作 bool getBoolForKey(const char* pKey); bool getBoolForKey(const char* pKey, bool

类型转换(CCstring int string char UTF-8互转)

在做数据转换时,最好包含以下头文件 #include <iostream> #include <cmath> #include <string> #include <sstream> USING_NS_CC; using namespace std; 在cocos2d-x中,也有一个格式刷:CCString(数据转换常常找她做中间人),那么我们要转换类型,可先将起始数据类型刷成CCString然后再转成目的数据类型,这个方法比较方便且实用. //int 转

【转载】cocos2d-x类型转换(CCstring int string char UTF-8互转)以及字符串详解

cocos2d-x中的字符串: <1>:使用 const char* 和 std::string const char* 是C风格的字符串  ,std::string 是C++风格的字符串,它封装了 const char * 初始化 std::string 对象: std::string name = "tony"; std:: string name = std::string("Micheal"); std::string 指针类型: std::st

cocos2d-x类型转换(CCstring int string char UTF-8互转)

http://www.cnblogs.com/leehongee/p/3642308.html //int 转 CCstring int num=5; CCString* ns=CCString::createWithFormat("%d",num); //CCstring 转 int int px = ns->intValue();//将CCString 转换为int的方法,转成float类型有 floatValue() //==========================

cocos2d-x wp8 中文显示问题

cocos2d-x项目在wp8下中文显示不了,有两个原因: 1. 本身字符串的utf8编码, 有问题. 2. 显示中文所需要的字体,缺失. 要解决第二个问题很简单,在fonts下添加一个中文字体,在显示的时候制定此中文字体 labelArcade = CCLabelTTF::create("中文字符串", "中文字体名称",字体大小); 至于第一个问题,说的是不能像上文那样直接用字符串写"中文字符串",解决方案有gb2312ToUnicode的方

Cocos2d-x滚动列表具体解释(CCScrollView的使用)

今天要写一个滚动列表功能,类似以下这样.(图片资源都是自己从天天酷跑里面抠的,仅用于学习方便) 首先,这样一个列表就和iOS里面的UITableView没什么两样,当然,Android中肯定也存在类似的控件. 在cocos2d-x引擎中參照ios中的UITableView实现了一个叫做CCTableView的类,用于创建列表,对于熟悉ios程序设计的人来说,这个内容应该是非常好理解的. 以下就介绍下CCTableView. 首先,mark几个比較好的博文. Cocos2d-x CCTableVi

cocos2d-x3.0 用CCDictionary写文件

bool CDownLoad_LocalData::WriteToConfigFile( DownLoadLocalData* downdata ){ CCDictionary* pDict = CCDictionary::create(); unsigned int lessonid = downdata->lession_id;char s_lessonid[10];sprintf(s_lessonid,"%d",lessonid); CCString* pValue1 =

【cocos2d-x 3.7 飞机大战】 决战南海I (二) 我方飞机的实现

在上一篇中.我们实现了游戏的開始界面,接下来要实现游戏的主界面.主界面包括地图.我方飞机.敌机等 先来实现我方飞机 我方飞机具有哪些属性呢? 飞机要具有生命值.要有动画效果(尾部喷气),飞机不可以飞出边界.所以要进行边界检測,当飞机生命值为0时.飞机会爆炸.然后被移除. .h文件 //飞机动画 Animate* planeFly(); //边界检測 void borderCheck(float dt); //飞机爆炸 void blowUp(); //移除飞机 void removePlane(

Cocos2d-x 3.x使用第三方库播放gif图

效果图: 由于kd的项目中需要用到gif,而Cocos2d-x本身是不支持gif播放的.百度 + google 了很久,最终都指向:https://github.com/opentalking/gif-for-cocos2dx 这里,可惜一开始看到的时候是基于2.2的,由于对底层代码不熟悉,根本没办法下手.于是联系原作者,他一个周末就搞出个3.x的版本,大家可以去膜拜下:地址:https://github.com/opentalking/gif-for-cocos2dx-3.x.git 在这里真