对应源码位置:(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是整个游戏对象的根节点
基本后面熟知的 sprite 、 layer 以及 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