- Cocos2dx3.2以后使用Vector<T>代替了CCArray。案例如下:
头文件:T02Vector.h |
#ifndef __T02Vector_H__ #define #include class { public: CREATE_FUNC(T02Vector); //Cocos2dx3.2以后使用Vector代替了CCArray Vector<Sprite*> bool }; #endif |
编写:T02Vector.cpp |
#include "T02Vector.h" //in cocos3.2 Vector代替CCArray //如果不是Ref的子类,那不能用Vector,应该用std::vector bool { Layer::init(); Sprite* //增加元素 _arr.pushBack(sprite); //遍历 Vector<Sprite*>::iterator for { Sprite* } for { } for { } //从后往前遍历 for { } //删除 _arr.eraseObject(sprite); return } |
2 Map的用法(注意字符编解码的第三方库有:iconv,cocos中集成的有这方面的功能)
头文件:T03Map.h |
#ifndef __T03Map_H__ #define #include class public: CREATE_FUNC(T03Map); bool }; #endif |
编写:T03Map.cpp |
#include "T03Map.h" /* ValueMap是用来代替cocos2d.x的CCDictionary */ bool { Layer::init(); //内容的加载 ValueMap& //CCDictionary* dict = CCDictionary::createWithContentsOfFile("about.xml"); //const CCString* x = dict->valueForKey("x"); //x->intValue(); //查找 auto if (it { CCLog("can } it = it->first; it->second; CCLog("key CCLog("............................end"); vm["中文"] CCLog("........start //遍历 for (auto { CCLog("key } CCLog("..........................end"); FileUtils::getInstance()->writeToFile(vm, #if 0 // C++11 for (auto { it.first; it.second; } // vm["aa"] // Value& v = 100; vm["bb"] #endif return } |
用到的about.xml如下: |
<?xml version="1.0" encoding="UTF-8" ?> <plist> <dict> <key>people1</key> <string>许佳音工作室出品</string> <key>people2</key> <string>总监:许佳音</string> <key>people3</key> <string>程序:姜博</string> <key>people4</key> <string>美术:马俊</string> <key>people5</key> <string>改编:班级</string> </dict> </plist> |
3
T04Label的用法
头文件:T04Label.h |
#ifndef __T04Label_H__ #define #include class public: CREATE_FUNC(T04Label); bool }; #endif |
编写:T04Label.cpp |
#include "T04Label.h" bool { Layer::init(); { Label* label->setString("12345"); addChild(label); label->setPosition(winSize.width } #if 0 Label* Label* Label* #endif //Label* label = Label::createWithTexture() return } |
运行结果: |
3
T05Touch触摸事件的用法
头文件:T05Touch.h |
#ifndef __T05Touch_H__ #define #include class { public: CREATE_FUNC(T05Touch); bool void }; #endif |
编写:T05Touch.cpp |
#include "T05Touch.h" bool { Layer::init(); { // EventListenerTouchOneByOne* ev->onTouchBegan // ev->onTouchEnded _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, } #if 0 { // EventListenerTouchOneByOne* ev->setSwallowTouches(true); ev->onTouchBegan _eventDispatcher->addEventListenerWithFixedPriority(ev, } #endif { Sprite* addChild(node); EventListenerTouchOneByOne* ev->onTouchBegan //通过touch->getLocation()的方式获得被选中的点的位置 Vec2 CCLog("Sprite return }; // // ev->onTouchEnded = CC_CALLBACK_2(T05Touch::TouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, } { EventListenerTouchAllAtOnce* ev->onTouchesBegan _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, } return } void } |