• cocos2dx中的三种基本的数据类型


    cocos2dx中提供了三种基本的数据类型:CCString(字符串),CCArray(数组),CCDictionary(数据字典(哈希的功能))

    2.CCString的用法

    class  CCString : public CCObject,可见CCString本质是一个CCObject,因此支持create方法和其他CCObject的特性

    a.CCString的创建:

    CCString *str=CCString("abc");

    CCString *str=CCString::create("1234");//CCObject的特性

    CCString *str=CCString::createwithformat("id%d",3);//格式化初始化

    static CCString* createWithData(const unsigned char* pData, unsigned long nLen);//使用二进制数据里创建一个字符串

    static CCString* createWithContentsOfFile(const char* pszFileName);//根据文件来创建一个字符串

    b.CCString与其他数据类型的转换:

    int intValue() const;

    unsigned int uintValue() const;

    float floatValue() const;

    double doubleValue() const;

    bool boolValue() const;

    2.CCArray是一个数组容器,用来盛放CCObject类型的对象

    a.CCArray的创建方法:

    /** 创建一个数组*/
    static CCArray* create();
    /** 使用一些对象创建数组*/
    static CCArray* create(CCObject* pObject, …);
    /** 使用一个对象创建数组*/
    static CCArray* createWithObject(CCObject* pObject);
    /** 创建一个指定大小的数组*/
    static CCArray* createWithCapacity(unsigned int capacity);
    /** 使用一个现有的CCArray 数组来新建一个数组*/
    static CCArray* createWithArray(CCArray* otherArray);

    b.CCArray中的删除操作

    需要注意的是,CCArray中的remove是把后续的整块数据全部一次性往前移动一个元素的内存距离,而fastremove只是把最后一个元素的内容替换当前要删除的元素的内容,会产生重复的数据:

    unsigned int remaining = arr->num - index;
    if(remaining>0)
    {
      memmove((void *)&arr->arr[index], (void *)&arr->arr[index+1],
      remaining * sizeof(CCObject*));
    }

    c.CCARRAY_FOREACH和CCARRAY_FOREACH_REVERSE在删除数据的遍历中,反向遍历不会出错,而前向遍历会报错,因为在本次遍历中,指针的位置是固定的,而中间有remove操作会移动整块数据块,导致末尾的指针置空,从而出错.故在可能需要删除数据的遍历中,应尽量使用CCARRY_FOREACH_REVEARE

    3.CCDictionary,数据字典

    a.数据字典里存储的都是键值对,且key的类型只支持int,或者string中的一种

    b.对于某部数据字典,其键值的类型只能唯一确定一种,不能既有int,又有string

    c.内部采用了哈希的原理,查找速度和数组差不多

    使用方法:

    CCDictionary *dict = CCDictionary::create(); //创建数据字典

     dict->retain();

     /*设置键值对*/

     dict->setObject(&str, "id");  //数据字典中存放的都是CCObject对象

    dict->setObject(&path, 222);//报错,一个数据字典中,key值必须一致,要么全是int,要么全是string,这里上面是string,这里是int,不行

     /*根据键名来获取值*/  

    CCString *id=(CCString*)dict->objectForKey("id");  

    //CCString *ph = (CCString*)dict->objectForKey("path");

     CCString *ph = (CCString*)dict->objectForKey(2);

     CCLog("%s", id->getCString());  

     CCLog("%s", ph->getCString());

    d.使用CCDictionary和xml文件来显示中文

     CCDictionary *file = CCDictionary::createWithContentsOfFile("chinese.xml");//通过文件来创建数据字典

     CCString *pep = (CCString *)file->objectForKey("people1");//该key是xml文件中已有的,否则报错

     CCLog("%s", pep->getCString());

     CCLabelTTF * ttf = CCLabelTTF::create(pep->getCString(), "Courier New", 30);

     ttf->setPosition(ccp(240, 160));

     addChild(ttf);

  • 相关阅读:
    多线程02
    多线程01
    CSS
    Mybatis CRUD中万能Map的用法及优势
    Tomcat配置
    Node.js+Vue+Webpack
    Java的几种常见排序算法
    maven插件 mybatis逆向工程
    ssm依赖
    mybatis spring整合依赖配置
  • 原文地址:https://www.cnblogs.com/ttss/p/4096983.html
Copyright © 2020-2023  润新知