Cocos2d-x Lua Node与Node层级架构
来源:互联网 发布:日本洗面奶推荐知乎 编辑:程序博客网 时间:2024/05/03 08:18
Cocos2d-x Lua采用层级(树形)结构管理场景、层、精灵、菜单、文本、地图和粒子系统等节点(Node)对象。一个场景包含了多个层,一个层又包含多个精灵、菜单、文本、地图和粒子系统等对象。层级结构中的节点可以是场景、层、精灵、菜单、文本、地图和粒子系统等任何对象。
节点的层级结构如下图所示。
节点的层级结构这些节点有一个共同的父类Node,Node类图如下图所示。Node类是Cocos2d-x Lua最为重要的根类,它是场景、层、精灵、菜单、文本、地图和粒子系统等类的根类。
Node类图Node中重要的操作
Node作为根类它有很多重要的函数下面我们分别介绍一下:
创建节点。local childNode = cc.Node:create()。
增加新的子节点。node:->addChild (childNode, 0, 123) ,第二个参数Z轴绘制顺序,第三个参数是标签。
查找子节点。local node = node:getChildByTag(123),通过标签查找子节点。
node:removeChildByTag(123, true) 通过标签删除子节点,并停止所有该节点上的一切动作。
node:removeChild(childNode, true) 删除childNode节点。并停止所有该子节点上的一切动作。
node:removeAllChildrenWithCleanup(true) 删除node节点的所有子节点,并停止这些子节点上的一切动作。
node:removeFromParentAndCleanup(true)从父节点删除node节点,并停止所有该节点上的一切动作。
Node中重要的属性
此外,Node还有两个非常重要的属性:position和anchorPoint。
position(位置)属性是Node对象的实际位置。position属性往往还要配合使用anchorPoint属性,为了将一个Node对象(标准矩形图形)精准的放置在屏幕某一个位置上,需要设置该矩形的锚点,anchorPoint是相对于position的比例,默认是(0.5,0.5)。我们看看下面的几种情况:
如下图所示是anchorPoint为(0.5,0.5)情况,这是默认情况。
anchorPoint为(0.5,0.5)下图所示是anchorPoint为(0.0,0.0)情况。
anchorPoint为(0.0,0.0)如下图所示是anchorPoint为(1.0,1.0)情况。
anchorPoint为(1.0,1.0)如下图所示是anchorPoint为(0.66, 0.5)情况。
anchorPoint为(0.66, 0.5)为了进一步了解anchorPoint使用,我们修改HelloLua实例,修改GameScene.lua的GameScene:createLayer()函数如下,其中加粗字体显示的是我们添加的代码。
function GameScene:createLayer()
cclog("GameScene init")
local layer = cc.Layer:create()
local label = cc.LabelTTF:create("Hello World", "Arial", 46)
label:setPosition(cc.p(size.width/2,
size.height - label:getContentSize().height))
label:setAnchorPoint(cc.p(1.0, 1.0))
layer:addChild(label)
local bg = cc.Sprite:create("HelloWorld.png")
bg:setPosition(cc.p(size.width/2, size.height/2))
layer:addChild(bg)
return layer
end
运行结果如下图所示,Hello World标签设置了anchorPoint为(1.0,1.0)。
Hello World标签的anchorPoint为(1.0,1.0)
游戏循环与调度
每一个游戏程序都有一个循环在不断运行,它是由导演对象来管理很维护。如果需要场景中的精灵运动起来,我们可以在游戏循环中使用定时器(Scheduler)对精灵等对象的运行进行调度。因为Node类封装了Scheduler类,所以我们也可以直接使用Node中定时器相关函数。
Node中定时器相关函数主要有:
scheduleUpdateWithPriorityLua(nHandler, priority)。每个Node对象只要调用该函数,那么这个Node对象就会定时地每帧回调用一次nHandler函数。priority是优先级,priority值越小越先执行。
unscheduleUpdate ()。停止scheduleUpdateWithPriorityLua的调度。
为了进一步了解游戏循环与调度的使用,我们修改HelloLua实例。修改GameScene.lua文件,代码如下:
require "Cocos2d"require "Cocos2dConstants"size = cc.Director:getInstance():getWinSize()local label①local GameScene = class("GameScene",function() return cc.Scene:create()end)function GameScene.create() local scene = GameScene.new() scene:addChild(scene:createLayer()) return sceneendfunction GameScene:ctor()end-- create layerfunction GameScene:createLayer() cclog("GameScene init") local layer = cc.Layer:create() label = cc.LabelTTF:create("Hello World", "Arial", 46) label:setPosition(cc.p(size.width/2, size.height - label:getContentSize().height)) label:setTag(123) label:setAnchorPoint(cc.p(1.0, 1.0)) layer:addChild(label) local bg = cc.Sprite:create("HelloWorld.png") bg:setPosition(cc.p(size.width/2, size.height/2)) layer:addChild(bg) local function update(delta)② local x,y = label:getPosition() label:setPosition(cc.p(x + 2, y - 2)) end --开始游戏调度 layer:scheduleUpdateWithPriorityLua(update, 0)③ function onNodeEvent(tag)④ if tag == "exit" then⑤ --开始游戏调度 layer:unscheduleUpdate()⑥ end end layer:registerScriptHandler(onNodeEvent)⑦ return layerendreturn GameScene
上述代码第①行定义了模块级标签对象label。代码第②行定义的update(delta)函数是调度函数。第③行代码layer:scheduleUpdateWithPriorityLua(update, 0)是开启游戏调度,按照帧率进行调度,优先级0是默认值。
第④行代码是层处理事件回调函数,其中第⑤行代码是判断是否为退出层事件,如果是退出层事件则调用第⑥行代码停止调度。第⑦行代码layer:registerScriptHandler(onNodeEvent)是注册层事件监听器。
更多内容请关注最新Cocos图书《Cocos2d-x实战:JS卷——Cocos2d-JS开发》
本书交流讨论网站:http://www.c欢迎加入Cocos2d-x技术讨论群:257760386
更多精彩视频课程请关注智捷课堂Cocos课程:http://v.51w智捷课堂现推出Cocos会员,敬请关注:http://v.51work6.com/courseInfoRedirect.do?action=netDetialInfo&courseId=844465&categoryId=0
《Cocos2d-x实战 JS卷》现已上线,各大商店均已开售:
京东:http://item.
0 0
- Cocos2d-x Lua Node与Node层级架构
- cocos2d-x中Node与Node层级架构
- 【Cocos2d-x】开发基础-Node与Node层级架构
- cocos2d-x Node
- cocos2d-x 有关Node
- Cocos2D-x Node知识集
- cocos2d-x 与 lua
- Cocos2d-x Lua工程架构
- cocos2d-x+lua+cocosbuilder+luaproxy开发中遇到的读取子node以及事件绑定问题
- cocos2d-x+lua+cocosbuilder+luaproxy开发中遇到的读取子node以及事件绑定问题
- cocos2d-x实现node圆弧运动
- bug cocos2d-x 3.1 Node::sortAllChildren
- Cocos2d-x 3.0 Schedule in Node
- Cocos2d-x 3.0-绘图节点Node
- cocos2d-x对Vector<Node*>排序
- cocos2d-x 与lua 通讯
- cocos2d-x lua与sqlite3
- Cocos2d-x 网络线程与UI线程的同步 继承Node但是不执行Update
- javascript学习八:函数的定义和传值
- 用gdb打印变量时显示No symbol "i" in current context
- How to judge the reused times of the film faced plywood
- 递归函数理解
- mongodb在windows下的安装步骤分享
- Cocos2d-x Lua Node与Node层级架构
- atos crash stack
- SVN 相关收藏
- IOS设计模式之一(MVC模式,单例模式)
- Cocos Studio 2.0.6下载地址
- 操作符new 和函数malloc()的异同及其用法
- org.springframework.web.util.IntrospectorCleanupListener
- zendStudio|Eclipse 相关收藏
- Eclipse重构代码