cocos2dx3.2以後使用vector<t>代替了ccarray。案例如下:
#ifndef
__t02vector_h__
#define
#include
"t32.h"
class
t02vector
: public
layer
{
public:
create_func(t02vector);
//cocos2dx3.2以後使用vector代替了ccarray
vector<sprite*>
_arr;
bool
init();
};
#endif
編寫:t02vector.cpp
"t02vector.h"
//in cocos3.2 vector代替ccarray
//如果不是ref的子類,那不能用vector,應該用std::vector
t02vector::init()
layer::init();
sprite*
sprite
= sprite::create();
//增加元素
_arr.pushback(sprite);
//周遊
vector<sprite*>::iterator
it;
for
(it
= _arr.begin();
it
!= _arr.end();
++it)
s
= *it;
}
(auto
!= _arr.end();++it)
it:
_arr)
//從後往前周遊
= _arr.rbegin();
!= _arr.rend();++it)
//删除
_arr.eraseobject(sprite);
return
true;
2 map的用法(注意字元編解碼的第三方庫有:iconv,cocos中內建的有這方面的功能)
頭檔案:t03map.h
__t03map_h__
t03map :
public
layer{
create_func(t03map);
編寫:t03map.cpp
"t03map.h"
/*
valuemap是用來代替cocos2d.x的ccdictionary
*/
t03map::init()
//内容的加載
valuemap&
vm =
fileutils::getinstance()->getvaluemapfromfile("about.xml");
//ccdictionary* dict = ccdictionary::createwithcontentsoffile("about.xml");
//const ccstring* x = dict->valueforkey("x");
//x->intvalue();
//查找
auto
it =
vm.find("aaa");
if (it
== vm.end())
cclog("can
not find aaa");
vm.find("people3");
it->first;
//key:的類型是std::string
it->second;
//value:的類型是value,相對cocos3.2.3的ccstring
cclog("key
is %s, value is %s",
it->first.c_str(),
it->second.asstring().c_str());
cclog("............................end");
vm["中文"]
= "bbb";
cclog("........start
walk over");
for (auto
vm.begin();
it !=
vm.end();++it)
is %s,value is %s",it->first.c_str(),it->second.asstring().c_str());
cclog("..........................end");
fileutils::getinstance()->writetofile(vm,
"new.xml");
#if 0
// c++11
it :
vm)
it.first;
it.second;
//
插入
vm["aa"]
= 10;
通路,這種通路有副作用,如果bb節點不存在,它會建立一個bb節點
value&
v =
vm["bb"];
v = 100;
vm["bb"]
= false;
用到的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
__t04label_h__
t04label :public
create_func(t04label);
編寫:t04label.cpp
"t04label.h"
t04label::init()
label*
label =
label::createwithcharmap("fonts/labelatlas.png",
24, 32, '0');
label->setstring("12345");
addchild(label);
label->setposition(winsize.width
/ 2, winsize.height
/ 2);
label::createwithbmfont();
label::createwithsystemfont("aaa",
"arial", 24);
label::createwithttf("");
//label* label = label::createwithtexture()
運作結果:
t05touch觸摸事件的用法
頭檔案:t05touch.h
__t05touch_h__
t05touch :public
create_func(t05touch);
void
touchended(touch*,event
*);
編寫:t05touch.cpp
"t05touch.h"
t05touch::init()
一般使用這種方式,和一個node相關聯
eventlistenertouchonebyone*
ev =
eventlistenertouchonebyone::create();
ev->ontouchbegan
= [](touch*,
event*){return
true; };
//
ev->ontouchended = [](touch*, event*){};
ev->ontouchended
= cc_callback_2(t05touch::touchended,
this);
_eventdispatcher->addeventlistenerwithscenegraphpriority(ev,
固有優先級的方式使用比較少
ev->setswallowtouches(true);
event*){cclog("touch
begin");
_eventdispatcher->addeventlistenerwithfixedpriority(ev,
-128);
node =
sprite::create();
addchild(node);
= [](touch*
touch,
event*){
//通過touch->getlocation()的方式獲得被選中的點的位置
vec2
pt =
touch->getlocation();
cclog("sprite
is touched, pt.x=%f, pt.y=%f",
pt.x,
pt.y);
// ev->ontouchended = cc_callback_2(t05touch::touchended, this);
node);
eventlistenertouchallatonce*
eventlistenertouchallatonce::create();
ev->ontouchesbegan
= [](const
std::vector<touch*>&,
event*){};
t05touch::touchended(touch*,