1. 需求:在所有子節點中得到是ui::Text類型的節點,並對其進行操作。
2. 解決方案:在根節點Node中有一個如下的函數:
/** * Gets the description string. It makes debugging easier. * @return A string * @js NA * @lua NA */
virtual std::string getDescription() const;
Node中默認的實現:
std::string Node::getDescription() const { return StringUtils::format("<Node | Tag = %d", _tag); }
我們在ui::Text中找到該函數的實現如下:
std::string Text::getDescription() const { return "Label"; }
修改為:
std::string Text::getDescription() const { return "cocos2d::ui::Text"; }
3.我們在遍歷子節點時就可以知道節點的類型是不是cocos2d::ui::Text了
for (Vector<Node*>::iterator it = all_children.begin(); it != all_children.end(); ++it){ Node* child = *it; std::string type_name = child->getDescription(); if (type_name == "cocos2d::ui::Text"){ //DO SOMETHING
} }
以上,完。