zoukankan      html  css  js  c++  java
  • cocos2dx 如何获得节点的类型

        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
                }
            }

        以上,完。

  • 相关阅读:
    PHP全部手册
    你必须收藏的GitHub技巧
    PV和并发
    api接口
    LeetCode 14. 最长公共前缀
    LeetCode 1037. 有效的回旋镖
    LeetCode 242. 有效的字母异位词
    LeetCode 151. 翻转字符串里的单词
    LeetCode 22. 括号生成
    LeetCode 面试题05. 替换空格
  • 原文地址:https://www.cnblogs.com/chevin/p/7157829.html
Copyright © 2011-2022 走看看