cpp-tests Node及NodeTest

本文介绍了cocos2dx中重要的Node类及其应用,详细讲解了NodeTest样例,包括CameraTest1的球形运动、CameraCenterTest的3D效果体验、Test2的锚点与孩子关系、Test6的removeChild功能以及ConvertToNode和NodeNormalizedPositionTest2中的节点转换和归一化位置测试。

~~~~我的生活,我的点点滴滴!!


一、Node类

Node 是cocos2dx中最常用,很重要的一个类,好多类都继承于他,这里我不一一列举了,所以有必要好好理解一下他。

/**
	Node 是场景元素的基类,场景中的元素基本都是Node的子类,像常用的Scene、Layer、Sprite、 Menu、Label等等。
	
	一、Node主要特性:
	
	1、Node能作为容器添加、删除任何他的子类,通过 addChild、getChildByTag、getChildByName、removeChild等。
	
	2、Node能定期安排执行回调函数,通过 schedule、unschedule等等。
	
	3、Node还能执行动作,通过runAction、stopAction等等。
	
	二、Node子类需要注意点:
	
	1、重载init去初始化资源与回调函数。
	
	2、创建回调函数去处理更新。
	
	3、重载draw去渲染自己。
	
	三、Node的一些基本属性:
	
	1、默认位置position为 x=0,y=0。
	
	2、默认缩放scale 为 1。
	
	3、默认旋转rotation为顺时针方向角度为0。
	
	4、默认锚点anchor为(0,0)。
	
	5、默认内容大小contentSize为(0,0)。
	
	6、默认是否可见visible为true。
	
	三、限制点:
	Node可以看作是一个“空的”物体,所以如果要在screen上画一些东西,要去继承Node或者使用Sprite(Sprite也是继承于Node)去重载draw()函数去在里面操作。
 */

Node内容太丰富了,丰富到说一天一夜也说不完,我写出一些我知道的、想知道、将来要知道的一些接口注释与注意点。

看下面源码:

class CC_DLL Node : public Ref
{
public:

	 /**
	  设置结点Node的本地zOrder顺序,他将要把所有的node放一块,按大小值排序,如果值相同,
	  按先添加在前面的原则,这样就是稳定的排序了。
	  */
    virtual void setLocalZOrder(int localZOrder);
	 
	 /**
	  定义了结点Node渲染的顺序,值越小越先渲染。全局zOrder相同的,渲染先后顺序是随机的。
	  唯一的意外是Node的全局zOrder不为0,因为0被场景使用了。全局zOrder主要用处可以按不
	  同的顺序来渲染其结点。
	  */
    virtual void setGlobalZOrder(float globalZOrder);
	
	 /**
	  设置在其父亲中的位置,他和OpenGL坐标系相同。
	  */
    virtual void setPosition(const Vec2 &position);

	 /**
	  归一化,他的值在0-1之间,他暗含setposition()这个动作。
	  伪代码:
	  void setNormalizedPosition(Vec2 pos)
	  {
		Size s = getParent()->getContentSize();
		_position = pos * s;
	  }
	  后面有一个例子讲其效果。
	  */
    virtual void setNormalizedPosition(const Vec2 &position);

    /** returns the normalized position */
    virtual const Vec2& getNormalizedPosition() const;

    /**
     * Sets the anchor point in percent.
     *
     * anchorPoint is the point around which all transformations and positioning manipulations take place.
     * It's like a pin in the node where it is "attached" to its parent.
     * The anchorPoint is normalized, like a percentage. (0,0) means the bottom-left corner and (1,1) means the top-right corner.
     * But you can use values higher than (1,1) and lower than (0,0) too.
     * The default anchorPoint is (0.5,0.5), so it starts in the center of the node.
     * @note If node has a physics body, the anchor must be in the middle, you cann't change this to other value.
     *
     * @param anchorPoint   The anchor point of node.
     */
	 /**
	   锚点是所有对本身操作的坐标值,像旋转、放大、缩小、转变等,就像一个钉子钉住一个物体,那物体就只能绕这个点做运动了。
	   锚点是归一化的坐标值,(0,0)左下角,(1,1)右上角,(0.5,0.5)中间,锚点是可以大于(1,1)和小于(0,0),如果这个node是一
	   个刚体,那么锚点必须在正中间(0.5,0.5),并且你不能改变其值。
	  */
    virtual void setAnchorPoint(const Vec2& anchorPoint);

	 /**
	  设置结点的原始大小,不管这个结点是放大缩小还是旋转,这个值是不会变,除非人为设置其他的值。
	  Layer和scene的contentsize就是屏幕的大小。
	  */
    virtual void setContentSize(const Size& contentSize);

    /**
     * Sets whether the anchor point will be (0,0) when you position this node.
     *
     * This is an internal method, only used by Layer and Scene. Don't call it outside framework.
     * The default value is false, while in Layer and Scene are true
     *
     * @param ignore    true if anchor point will be (0,0) when you position this node
     * @todo This method should be renamed as setIgnoreAnchorPointForPosition(bool) or something with "set"
     */
	 /**
		设置锚点始终为(0,0),不管怎么设置node的位置,仅仅在Layer和Scene上使用。
		默认为false不开启的,但是在Layer和Scene中是默认为true。
	  */
    virtual void ignoreAnchorPointForPosition(bool ignore);

	 /**
	  添加其到node容器中,默认zOrder为0,他会retain一次,所以如果使用addChild不需要手动retain。
	  如果添加到一个running的node中,会立刻调用'onEnter'和'onEnterTransitionDidFinish'
	  */
    virtual void addChild(Node * child);

	 /**
	  带有本地zOrder值添加到node容器中,其他同上。
     */
    virtual void addChild(Node * child, int localZOrder);
	 
	 /**
      同上。
     */
     virtual void addChild(Node* child, int localZOrder, int tag);
	 
	 /**
      添加一个name为标签,后续可以通过name来查找。
     */
    virtual void addChild(Node* child, int localZOrder, const std::string &name);
	
    /**
     通过tag标签,得到其对象指针。
     */
     virtual Node * getChildByTag(int tag) const;
	 
    /**
     通过name标签,得到其对象指针。
     */
    virtual Node* getChildByName(const std::string& name) const;
	
    
    /** Search the children of the receiving node to perform processing for nodes which share a name.
     *
     * @param name The name to search for, supports c++11 regular expression.
     * Search syntax options:
     * `//`: Can only be placed at the begin of the search string. This indicates that it will search recursively.
     * `..`: The search should move up to the node's parent. Can only be placed at the end of string
     * `/` : When placed anywhere but the start of the search string, this indicates that the search should move to the node's children.
     *
     * @code
     * enumerateChildren("//MyName", ...): This searches the children recursively and matches any node with the name `MyName`.
     * enumerateChildren("[[:alnum:]]+", ...): This search string matches every node of its children.
     * enumerateChildren("A[[:digit:]]", ...): This searches the node's children and returns any child named `A0`, `A1`, ..., `A9`
     * enumerateChildren("Abby/Normal", ...): This searches the node's grandchildren and returns any node whose name is `Normal`
     * and whose parent is named `Abby`.
     * enumerateChildren("//Abby/Normal", ...): This searches recursively and returns any node whose name is `Normal` and whose
     * parent is named `Abby`.
     * @endcode
     *
     * @warning Only support alpha or number for name, and not support unicode
     *
     * @param callback A callback function to execute on nodes that match the `name` parameter. The function takes the following arguments:
     *  `node` 
     *      A node that matches the name
     *  And returns a boolean result. Your callback can return `true` to terminate the enumeration.
     *
     * @since v3.2
     */
	 /**
	  新增的函数功能,通过名字正则来搜索所有符合条件指针,cpp-test里面的demo貌似运行很卡。
	  */
    virtual void enumerateChildren(const std::string &name, std::function<bool(Node* node)> callback) const;
	
    /**
     上面说过会Node里面会有一个容器来保存添加进来的孩子对象指针,
	 使用的是cocos2dx自己封装的Vector容器。
     */
    virtual Vector<Node*>& getChildren() { return _children; }
    virtual const Vector<Node*>& getChildren() const { return _children; }
   
    ////// REMOVES //////

    /**
     * Removes this node itself from its parent node with a cleanup.
     * If the node orphan, then nothing happens.
     * @see `removeFromParentAndCleanup(bool)`
     */
	 /**
      从其父结点中移除自己,并且cleanup,cleanup执行的是移除所有的和此node有关的任何操作,
	  像调试器,回调函数、正在运行的动作等等,此接口就是removeFromParentAndCleanup(true);
     */
    virtual void removeFromParent();
    /**
     * Removes this node itself from its parent node.
     * If th
<think>好的,我现在需要帮助用户解决在编译aws-sdk-cpp时遇到的std::regex_error错误,特别是涉及到SmithyClientTest.cpp的问题。首先,我需要回顾之前的对话,确保理解上下文。用户之前询问了如何排除某个特定文件的编译,现在新的问题是关于std::regex_error的错误。 首先,我需要分析std::regex_error的可能原因。根据经验,这种错误通常与正则表达式的语法或编译器对C++11正则表达式的支持有关。用户提到的是在编译aws-sdk-cpp时出现的问题,尤其是在SmithyClientTest.cpp中,所以可能该测试文件中的某个正则表达式触发了错误。 接下来,我需要考虑用户的环境。用户可能使用的是较旧的GCC版本(比如GCC 4.9之前),因为早期版本的GCC对C++11正则表达式的支持不完全,容易导致std::regex_error。或者,用户可能在Windows上使用MinGW,这也可能导致类似的问题。 接下来,我需要参考用户提供的站内引用。引用[1]提到了智能指针和互斥锁,这可能与资源管理有关,但当前问题涉及正则表达式,可能关联不大。引用[2]提到CMake构建与Visual Studio的链接,这可能提示用户可能在Windows环境下使用CMake进行编译,但需要进一步确认。 回顾用户之前的问题,用户尝试通过CMake配置排除特定代码行或文件。现在的问题可能与代码中的正则表达式有关,可能需要修改代码或调整编译选项。例如,可以尝试禁用相关的测试用例,或者升级编译器以支持更好的正则表达式实现。 可能的解决方案包括: 1. 禁用涉及正则表达式的测试用例,特别是SmithyClientTest.cpp中的相关部分。 2. 升级编译器到支持C++11正则表达式的版本(如GCC 5以上或Clang 3.4以上)。 3. 修改代码中的正则表达式,确保语法正确且与当前编译器兼容。 4. 使用其他正则表达式库(如Boost.Regex)替换标准库的实现。 我需要验证这些可能性,并给出具体的步骤。例如,如何通过CMake排除测试文件,或者如何替换正则表达式库。同时,需要确保回答结构清晰,步骤详细,帮助用户逐步解决问题。 另外,用户可能希望尽可能少地修改代码,因此优先考虑通过CMake配置来禁用测试,或者调整编译器标志。如果用户的环境允许,升级编译器可能是根本解决办法,但用户可能需要临时解决方案。 最后,需要生成相关问题,帮助用户进一步解决可能遇到的类似问题,例如如何处理其他编译错误,如何优化构建配置等。</think>### 分步解决方案:解决aws-sdk-cpp编译时的`std::regex_error`错误 --- ### **问题根源分析** `std::regex_error`通常由以下原因导致: 1. **正则表达式语法错误**:正则表达式字符串不符合C++标准语法[^1]。 2. **编译器兼容性问题**:旧版本编译器(如GCC < 5或Clang < 3.4)对C++11正则表达式支持不完善[^2]。 3. **Unicode处理异常**:某些环境下字符编码与正则表达式引擎不兼容。 --- ### **方法1:禁用`SmithyClientTest.cpp`中的问题代码** #### 步骤1:通过CMake排除文件编译 在AWS SDK的`CMakeLists.txt`中定位相关测试文件,并注释其包含: ```cmake # 原始配置(位于aws-cpp-sdk-core-tests的CMakeLists.txt) file(GLOB CORE_TEST_SOURCE "smithy/client/*.cpp" # 注释或移除SmithyClientTest.cpp # "smithy/client/SmithyClientTest.cpp" ... ) ``` #### 步骤2:重新生成构建系统 ```bash rm -rf build && mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release # 清理旧构建并重新配置 make aws-cpp-sdk-core # 仅编译核心模块(跳过测试) ``` --- ### **方法2:修复正则表达式兼容性** #### 步骤1:检查问题代码中的正则表达式 在`SmithyClientTest.cpp`中搜索`std::regex`相关代码,例如: ```cpp // 示例:可能存在问题的正则表达式 std::regex pattern("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"); ``` #### 步骤2:替换为兼容语法 调整正则表达式以适配旧编译器: ```cpp // 修改为明确转义或简化语法 std::regex pattern("[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z"); ``` --- ### **方法3:强制使用C++11及以上标准** #### 步骤1:在CMake中显式设置C++标准 在`aws-sdk-cpp/CMakeLists.txt`中添加: ```cmake set(CMAKE_CXX_STANDARD 11) # 或更高版本(14/17) set(CMAKE_CXX_STANDARD_REQUIRED ON) ``` #### 步骤2:检查编译器版本 ```bash g++ --version # 确认GCC >= 5 或 Clang >= 3.4 ``` --- ### **方法4:替换正则表达式库(高级)** #### 步骤1:链接Boost.Regex库 安装Boost并修改CMake配置: ```cmake find_package(Boost COMPONENTS regex REQUIRED) target_link_libraries(aws-cpp-sdk-core PRIVATE Boost::regex) ``` #### 步骤2:重定义`std::regex`命名空间 在代码中添加预处理指令: ```cpp #include <boost/regex.hpp> namespace std { using boost::regex; using boost::regex_error; } // 仅限测试代码 ``` --- ### **验证与调试** 1. **编译日志过滤** 使用`grep`定位错误来源: ```bash make 2>&1 | grep -C 10 "regex_error" ``` 2. **最小化复现代码** 提取触发错误的代码片段单独测试: ```cpp #include <regex> int main() { std::regex test("[a-z]+\\d*"); return 0; } ``` --- ### 相关问题 1. **如何检测C++编译器的正则表达式支持?** 通过预定义宏(如`__GNUC__`)判断编译器版本。 2. **AWS SDK中有哪些模块依赖正则表达式?** Smithy模型解析和HTTP头处理模块可能涉及正则操作[^3]。 3. **CMake中如何按条件排除特定平台代码?** 使用`if()`语句结合`CMAKE_SYSTEM_NAME`或`CMAKE_CXX_COMPILER_ID`[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值