cpp-tests Node及NodeTest

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

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


一、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)`
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值