5.【cocos2d-x 源码分析】:Node 类体系的详细分析

对应源码位置:(1)cocos2d-x-3.3\cocos\2d\CCNode (2)cocos2d-x-3.3\cocos\2d\CCLayer(3)cocos2d-x-3.3\cocos\2d\CCScene
Node是整个游戏对象的根节点

基本后面熟知的 spritelayer 以及 scene都是他的子类。
在这里插入图片描述

Node类的部分代码

由于Cocos2d-x采用继承的方针组织整个游戏对象,所以Node类的负担很重,很庞大。简单抽取一部分出来。

class CC_DLL Node : public Ref
{
   
public:
    /// Default tag used for all the nodes
    static const int INVALID_TAG = -1;
	//老规矩  就是看 变了没  没变就不计算  这样优化
    enum {
   
        FLAGS_TRANSFORM_DIRTY = (1 << 0),
        FLAGS_CONTENT_SIZE_DIRTY = (1 << 1),

        FLAGS_DIRTY_MASK = (FLAGS_TRANSFORM_DIRTY | FLAGS_CONTENT_SIZE_DIRTY),
    };
    
    static Node * create();
	//加入 孩子节点  用一个vector来存储
    virtual void addChild(Node* child, int localZOrder, const std::string &name);
	//设置 父级节点
    virtual void setParent(Node* parent);
   	//这里主要是  设置 opengl的 shader程序  下一篇具体分析
    GLProgram* getGLProgram() const;
    GLProgramState *getGLProgramState() const;
    virtual void setGLProgramState(GLProgramState *glProgramState);
	//这里就是 具体渲染时  跟opengl有关的部分 
	//统一的接口  代表节点对象的绘制 方式
    virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags);
    virtual void draw() final;

    /**
     * Visits this node's children and draw them recursively.
     */
     //这里是  访问孩子节点  并绘制  后面具体分析
    virtual void visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags);
    virtual void visit() final;
	//获取 Node所属于的scene
    virtual Scene* getScene() const;
    //这个就是  前面schedule里面注册的  每帧调用的函数  注册的位置
    //这也是  最常用的 update逻辑
    void scheduleUpdate(void);
    virtual void update(float delta);

	//下面是 node的属性  也是action动作改变的数据

    float _rotationX;               ///< rotation on the X-axis
    float _rotationY;               ///< rotation on the Y-axis

    // rotation Z is decomposed in 2 to simulate Skew for Flash animations
    float _rotationZ_X;             ///< rotation angle on Z-axis, component X
    float _rotationZ_Y;             ///< rotation angle on Z-axis, component Y

    float _scaleX;                  ///< scaling factor on x-axis
    float _scaleY;                  ///< scaling factor on y-axis
    float _scaleZ;                  ///< scaling factor on z-axis

    Vec2 _position;                ///< position of the node
    float _positionZ;               ///< OpenGL real Z position
    Vec2 _normalizedPosition;
    bool _usingNormalizedPosition;
    bool _normalizedPositionDirty;

    float _skewX;                   ///< skew angle on x-axis
    float _skewY;                   ///< skew angle on y-axis

    Vec2 _anchorPointInPoints;     ///< anchor point in points
    Vec2 _anchorPoint;             ///< anchor point normalized (NOT in points)

    Size _contentSize;              ///< untransformed size of the node
    bool _contentSizeDirty;         ///< whether or not the contentSize is dirty

    Mat4 _modelViewTransform;    ///< ModelView transform of the Node.

    // "cache" variables are allowed to be m
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值