【Cocos2d-x】开发基础-Node与Node层级架构
本篇博客讲解: Node与Node层级架构首先来看一张图 这个树形结构,其实就是一种包含关系 Scene* HelloWorld::createScene()
{
// 创建场景对象
auto scene = Scene::create();
// 创建层对象
auto layer = HelloWorld::create();
// 把层放到场景里
scene->addChild(layer);
// 返回这个场景
return scene;
}
创建场景以及HelloWord这个层,然后把层放到场景里 this->addChild(menu,1);//把菜单放到当前层
//把标签加入到层中
this->addChild(label,1);
//把图片精灵加入到层中
this->addChild(sprite,0);
代码在HelloWord.cpp中可以看到 层与精灵也是一对多的关系,然后层与菜单,粒子系统,瓦片地图都是一对多的关系 Node中重要的操作由于场景、层、菜单、精灵…都是继承的Node,所以它们有一些共同的属性和方法 创建节点Node* childNode = Node::create()
增加新的子节点node->addChild(childNode,0,123)
第一个参数是添加的节点;第二个参数是添加的Node的z轴的顺序(添加节点的时候会有顺序的,相当于绘制的时候的先后顺序);第三个参数是Tag/标签,可以理解成id,通过这个tag来操作node对象 LocalZOrder is the 'key' used to sort the node relative to its siblings.
The Node's parent will sort all its children based on the LocalZOrder value. If two nodes have the same LocalZOrder,then the node that was added first to the children's array will be in front of the other node in the array.
Also,the Scene Graph is traversed using the "In-Order" tree traversal algorithm ( http://en.wikipedia.org/wiki/Tree_traversal#In-order ) And Nodes that have LocalZOrder values < 0 are the "left" subtree While Nodes with LocalZOrder >=0 are the "right" subtree.
addChild有4个方法 virtual void addChild ( Node * child)
virtual void addChild ( Node * child,int localZOrder)
virtual void addChild ( Node * child,int localZOrder,int tag)
virtual void addChild ( Node * child,const std::string & name)
查找子节点Node* node = node->getChildByTag(123)
删除子节点node->removeChildByTag(123,true)//第二个参数表示是否在内存中清除这个对象
node->removeChild(childNode)//直接删除节点
node->removeAllChildrenWithCleanup(true) //true-清除内存
node->removeFromParentAndCleanup(true) //true-清除内存
Node中重要的属性Node两个非常重要的属性: 2.anchorPoint为(0,0),也就是精灵图片的左下角 3.anchorPoint为(1.0,1.0),也就是精灵图片的右上角 4.anchorPoint为(0.66,0.5),这个具有普遍性 游戏循环与调度每一个游戏程序都有一个循环在不断运行,它是有导演对象来管理和维护。 void scheduleUpdate ( void )//开始游戏调度,每个Node对象只要调用该函数,那么这个Node对象就会定时地每帧回调用一次自己的update(float dt)函数,每帧调用一次update函数!这是固定的时间和函数
//下面这个函数更加个性化,可以自定义回调函数和时间
void schedule ( SEL_SCHEDULE selector,float interval )//与scheduleUpdate函数功能一样,不同的是我们可以指定回调函数(通过selector指定),也可以根据需要指定回调时间间隔
//一旦调度开始,就会不断循环
void unscheduleUpdate ( void )//停止update(float dt)函数调度 和scheduleUpdate对应
void unschedule ( SEL_SCHEDULE selector )//可以指定具体函数停止调度 和schedule对应
void unscheduleAllSelectors ( void )//可以停止所有调度
源代码下载地址:GITHUB源码下载地址: 【点我进行下载】 本文章由[谙忆]编写, 所有权利保留。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |