1, Ccarray of the traversal
See here, some people also press can not endure to raise watermelon knife: You do not say after 3.0beta has no ccarray this goods? What are you doing now?
In fact, I am also very innocent,Ccarray really is not, but in some unknown corner but hidden __array this goods, I dare to guess it is ccarray half brother, because it has and ccarray the same function. Unfortunately, because of the rise of vectors, __array is doomed to be buried.
Ccarray's traversal macro is Ccarray_foreach (_array,_object), which is the way to traverse, with a for statement to explain the beginning of the int i=0. Use the following:
View Sourceprint?
01.
auto sp = Sprite::create(
"close.png"
);
//建立俩精灵
02.
sp->setPosition(Point(
200
,
300
));
03.
this
->addChild(sp,
2
);
04.
05.
auto sp2 = Sprite::create(
"close.png"
);
06.
sp2->setPosition(Point(
300
,
300
));
07.
this
->addChild(sp2,
2
);
08.
09.
__Array* m_array = __Array::create();
//用__Array创建一个数组
10.
m_array->addObject(sp);
//将sp与sp2放入数组中
11.
m_array->addObject(sp2);
12.
13.
Ref* obj = nullptr;
14.
CCARRAY_FOREACH(m_array,obj)
15.
{
16.
auto sp_obj = static_cast<Sprite*>(obj);
//将obj强制转换成Sprite
17.
sp_obj->runAction(MoveBy::create(
3
.0f,Point(
250
,
250
)));
//运动
18.
}
Of course, some people like the more exciting way to come from behind! Is there such a traversal? Of course, Ccarray_foreach_reverse (_array,_object) satisfies all your fantasies. in the words of SB.: From the back. Pain! Fast!. Is that really so much fun? Just try it and know it (if you want to have a girlfriend ...) Strange, why do I talk about girlfriends?? )。
2, ccdictionary of the traversal
I beg you again to put down the angry watermelon knife in your hand! If Ccarray and __array are half-mother, then Ccdictionary and __dictionary are the same!
ccdictionary Traverse macro: Ccdict_foreach (_dic,_ele), use the following:
View Sourceprint?
01.
__Dictionary* dic = __Dictionary::create();
02.
dic->setObject(sp,
"sp"
);
//将sp与sp2放入字典中,键分别为sp与sp2
03.
dic->setObject(sp2,
"sp2"
);
04.
05.
DictElement* ele;
06.
CCDICT_FOREACH(dic,ele)
07.
{
08.
auto sp_obj = static_cast<Sprite*>(ele->getObject());
//将ele强制转换成Sprite
09.
sp_obj->runAction(MoveBy::create(
3
.0f,Point(
250
,
250
)));
10.
}
3, vector "traversal"
The traversal of a vector involves c++11, and a relationship with C + + 11 means that the code can be written in a simpler, less reliable way. Look underneath.
View Sourceprint?
1.
Vector<Sprite*> sp_vec;
//创建一个容器
2.
sp_vec.pushBack(sp);
//将俩精灵扔进去
3.
sp_vec.pushBack(sp2);
4.
5.
for
( auto sp_obj : sp_vec)
6.
{
7.
sp_obj->runAction(MoveBy::create(
3
.0f,Point(
250
,
250
)));
//每次for循环都从sp_vec容器中取出一个精灵
8.
}
One step, there are wood! if the use of vectors is not very familiar with children's shoes, poke here : http://blog.csdn.net/start530/article/details/19170853
4. "Traversal" of map
It is similar to the way vectors are traversed, but there are some things that are different:
View Sourceprint?
01.
Map<std::string,Sprite*> sp_map;
//创建一个Map容器,键值分别为string型与Sprite类型
02.
sp_map.insert(
"sp"
,sp);
//将sp扔进去,
03.
sp_map.insert(
"sp2"
,sp2);
04.
05.
for
( auto e : sp_map)
06.
{
07.
auto sp_obj = e.second;
//e.first获得的是e的键值,e.second获得对象
08.
sp_obj->runAction(MoveBy::create(
3
.0f,Point(
250
,
250
)));
09.
}
Not familiar with the map? Poke here: http://blog.csdn.net/start530/article/details/19284301
5. Use the range for statement
The use of vector and map above uses a new thing: the range for statement. Since it's all written here, I'll keep on talking about the words.
The scope for statement is a new statement provided by C++11, which is in the following form:
for (declaratio:expression)
{
Statement
}
Expression is an object, such as a vector, Declarationo is responsible for defining a variable that will be initialized to the next element in expression each time it is iterated.
So what types of objects can use the range for statement? At present I know can use:vector,map,std::string, there are cocos2dx in the Vector,map. These common features are the begin and end members that have the ability to return iterators.
Here's an example: Use the range for statement to output the characters in a string object one line at a std::string:
View Sourceprint?
1.
std::string str(
"star530 is so cool"
);
2.
//每行输出str中的一个字符
3.
for
(auto c : str)
//对于str中的每个字符
4.
cout << c << endl;
//输出当前字符,后面紧跟一个换行符
The type of C is determined by using auto (Type C is char). Each time the loop is passed, the next character of Str is copied to C.
cocos2dx3.0 transition of various traversal and scope for statements using "go"