第四章数据结构-Dictionary

本文详细介绍了Cocos2d-x中的_Dictionary数据结构,包括如何创建、添加、移除和查找元素,以及其他操作和遍历方法。_Dictionary用于存储键值对,键可以是字符串或整数,值必须是Ref或其子类对象。通过 CCDICT_FOREACH 宏可以方便地遍历_Dictionary内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cocos2d-x学习笔记


_Dictionary数据结构

_Dictionary类是模仿Objective-C中的NSDictionay类而设计的,通过引用计数管理内存。 _Dictionary继承于Ref类,因此它所能容纳的是Ref及子类所创建的指针。

创建_Dictionary对象

  • static _Dictionary * create:创建_Dictionary。
  • static _Dictionary * createWithDictionary(_Dictionary * srcDict):用一个已经存在的_Dictionary 来创建另一个新的 _Dictionary。
  • static _Dictionary * createWithOfFile(const char * pFileName):从属性列表文件创建_Dictionary。

添加元素

向_Dictionary对象中添加元素必须是“键-值”对,“键”可以使字符串(std::string)类型或者整数(signed int)类型,而“值”必须是Ref和子类的对象指针类型。

  • void setObject(Ref * pObject, const std::stirng& key):插入一个“键-值”对,其中pObject是“值”,key是“键”。如果是第一次调用,_Dictionary的“键”类型是字符串类型,之后就不能插入整型”键“。如果已经存在该”键“,则旧“键-值”对会被释放和移除,被新的替代。
  • void setObject(Ref * pObject, intptr_r key):插入一个“键-值”对,其中pObject是“值”,key是“键”,intprt_r类型是signed_int类型的别名。如果是第一次调用,_Dictionary的“键”类型是整型,之后就不能插入字符串类型“键”,如果已经存在该”键“,则旧“键-值”对会被释放和移除,被新的替代。

移除元素

  • void removeObjectForKey(const std::string& key):通过指定键移除元素。
  • void removeObjectForKey(intptr_t key):通过指定键移除元素。
  • void removeObjectForKeys(_Array *pKeyArray):通过一个_Array中键集合移除元素。
  • void removeObjectForElement(DictElement * pElement):通过指定元素来移除。
  • void removeAllObject():移除所有的元素。

查找元素

  • Ref * objectForKey(const std::string& key):返回指定字符串类型“键”的“值”。
  • Ref * objectForKey(intptr_t key):返回指定整型“键”的“值”。
  • const _String * valueForKey(const std::string& key):返回指定字符串类型“键”的“值”,返回的是_String指针类型,(这里是假定_Stirng类型)如果不是活着没找到,怎返回空值。
  • const _String * valueForKey(intptr_r key):返回指定整型“键”的“值”,放回是_String指针类型,(这里是假定_Stirng类型)如果不是或者没找到,怎返回空值。

其他操作函数

  • _Array * allKey():返回一个包含所有“键”的“值”的_Array数据结构。
  • unsigned int count():返回元素个数。
  • bool writeToFile(const char * fullPath):把_Dictionary写到一个属性列表文件中,写入的“值”要求是字符串类型。

遍历_Dictionary数据结构

  • Cocos2d-x提供一个宏:CCDICT_FOREACH

实例

HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

#define MAX_COUNT 50

class HelloWorld : public cocos2d::Layer
{
    cocos2d::__Dictionary* dic1;
    cocos2d::__Dictionary* dic2;

public:
    ~HelloWorld();
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorld.cpp文件

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    auto goItem = MenuItemImage::create("CloseNormal.png",
        "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    goItem->setPosition(Vec2(origin.x + visibleSize.width - goItem->getContentSize().width / 2, origin.y + goItem->getContentSize().height / 2));

    auto menu = Menu::create(goItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

    this->dic1 = __Dictionary::create();
    this->dic1->retain();
    this->dic2 = __Dictionary::create();
    this->dic2->retain();

    for (int i = 0; i < MAX_COUNT; i++)
    {
        auto sprite1 = Sprite::create("Cherry.png");
        this->dic1->setObject(sprite1, i);

        auto sprite2 = Sprite::create("water.png");
        __String * key = __String::createWithFormat("key % d", i);
        this->dic2->setObject(sprite2, key->getCString());
    }

    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    /*Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif*/
    Size visibleSize = Director::getInstance()->getVisibleSize();

    DictElement * pElement;

    CCDICT_FOREACH(dic1, pElement)
    {
        int key = pElement->getIntKey();
        log("Add Sprite %d", key);

        Sprite* sprite = (Sprite *)pElement->getObject();

        int x = CCRANDOM_0_1() * visibleSize.width;
        int y = CCRANDOM_0_1() * visibleSize.height;

        sprite->setPosition(Vec2(x, y));
        this->removeChild(sprite);
        this->addChild(sprite);
    }

    CCDICT_FOREACH(dic2, pElement)
    {
        const char * key = pElement->getStrKey();
        log("Add Sprite % s", key);

        Sprite * sprite = (Sprite *)pElement->getObject();
        int x = CCRANDOM_0_1() * visibleSize.width;
        int y = CCRANDOM_0_1() * visibleSize.height;

        sprite->setPosition(Vec2(x, y));
        this->removeChild(sprite);
        this->addChild(sprite);
    }
}

HelloWorld::~HelloWorld()
{
    this->dic1->removeAllObjects();
    CC_SAFE_RELEASE_NULL(this->dic1);

    this->dic2->removeAllObjects();
    CC_SAFE_RELEASE_NULL(this->dic2);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值