Cocos2d-x 核心概念 - 游戏循环与调度
每一个游戏程序都有一个循环在不断运行,他是由导演对象来管理和维护。
如果需要场景中的精灵运动起来,我们可以在循环中使用定时器(Scheduler)对精灵等对象的运行进行调度
因为Node中封装了 Scheduler 类,所以我们可以直接使用Node中定时器相关函数
schedulerUpdateWithPriorityLua ( nHandler , priority) --每个Node对象只要调用该函数,那么这个Node对象就会定时的每帧调用一次nHandler 函数,priority是优先级,越小越先被执行 unschedulerUpdate(): --停止schedulerUpdateWithPriorityLua 的调度
function MainScene:createLayer()
local layer = cc.Layer:create()
--设置背景
local bg = cc.Sprite:create("HelloWorld.png")
bg:setPosition(cc.p(size.width/2,size.height/2))
bg:setAnchorPoint(0.5,0.5)
layer:addChild(bg)
--创建一个label
local textType = cc.Label:createWithSystemFont("Hello World","Arial",20)
textType:setPosition(cc.p(size.width/2,size.height*3/4))
textType:setAnchorPoint(0.5,0.5)
textType:setTextColor(cc.c4b(0,0,255,255))
layer:addChild(textType)
--定义update(delta)调度函数
local function update(delta)
local x,y = textType:getPosition()
textType:setPosition(cc.p(x+2,y-2))
end
--开始游戏调度
layer:scheduleUpdateWithPriorityLua(update,0) --开启游戏调度,按照频率进行调度,0是默认值
local function onNodeEvent(tag) --层处理事件回调函数
if tag == "exit" then --判断是否推出层处理事件
layer:unscheduleUpdate() --代码停止调度
end
end
layer:registerScriptHandler(onNodeEvent) --注册层事件监听器
return layer
end
下面的代码将实现文本滚动效果
local size = cc.Director:getInstance():getWinSize()
local MainScene = class("MainScene",function()
return cc.Scene:create()
end)
function MainScene.create()
local scene = MainScene.new()
scene:addChild(MainScene:createLayer())
return scene
end
function MainScene:createLayer()
local layer = cc.Layer:create()
--设置背景
local bg = cc.Sprite:create("HelloWorld.png")
bg:setPosition(cc.p(size.width/2,size.height/2))
bg:setAnchorPoint(0.5,0.5)
layer:addChild(bg)
--创建一个label
local textType = cc.Label:createWithSystemFont("Hello World","Arial",20)
textType:setPosition(cc.p(size.width/2,size.height*3/4))
textType:setAnchorPoint(0,0.5)
textType:setTextColor(cc.c4b(0,0,255,255))
layer:addChild(textType)
--[[
一、定义调度函数update ----循环体
二、开始游戏调度 ----循环头
三、层处理事件回调函数 ----判断
四、注册层事件监听器 ----注册
]]
local function update(delta)
local x,y = textType:getPosition()
if x <size.width and x>= 0 then
textType:setPosition(cc.p(x+2,y))
else
if x >= size.width then
textType:setAnchorPoint(0,0.5)
x,y = textType:getPosition()
if x <size.width and x>= 0 then
textType:setPosition(cc.p(x+2,y))
else
textType:setAnchorPoint(1,0.5)
textType:setPosition(cc.p(0,y))
end
end
end
end
layer:scheduleUpdateWithPriorityLua(update,0)
local function onNode(tag)
if tag == "exit" then
layer:unscheduleUpdate()
end
end
layer:registerScriptHandler(onNode)
return layer
end
return MainScene
Cocos2d-x 核心概念 - 游戏循环与调度的更多相关文章
- cocos2d-x游戏循环与调度
每一个游戏程序都有一个循环在不断运行,它是有导演对象来管理很维护.如果需要场景中的精灵运动起来,我们可以在游戏循环中使用定时器(Scheduler)对精灵等对象的运行进行调度.因为Node类封装了Sc ...
- cocos2d-x游戏循环和日程安排
每场比赛有一个程序执行周期继续.这是导演对象来管理非常维修.在运动假设需要的场景精神,我们可以使用计时器在游戏圈(Scheduler)精灵和其他对象进行调度. 由于Node类封装了Scheduler类 ...
- WebGPU 导入[2] - 核心概念与重要机制解读
目录 1. 核心概念 ① 适配器和设备 ② 缓冲.纹理.采样器 ③ 绑定组 ④ 着色器与管线 ⑤ 编码器与队列 2. 重要机制 ① 缓冲映射机制 ② 时间线 1. 核心概念 这部分不会详细展开,以后写 ...
- 刀哥多线程GCD核心概念gcd
GCD GCD 核心概念 将任务添加到队列,并且指定执行任务的函数 任务使用 block 封装 任务的 block 没有参数也没有返回值 执行任务的函数 异步 dispatch_async 不用等待当 ...
- cocos2d-x一些核心概念截杀
Cocos2d-x中有很多概念,这些概念很多来源于动画.动漫和电影等行业,例如:导演.场景和层等概念,当然也有些有传统的游戏的概念.Cocos2d-x中核心概念:导演, 场景,层,节点,精灵,菜单动作 ...
- 【Visual C++】游戏编程学习笔记之三:游戏循环的使用
本系列文章由@二货梦想家张程 所写,转载请注明出处. 本文章链接:http://blog.csdn.net/terence1212/article/details/44208419 作者:Zee ...
- JAVAEE——BOS物流项目13:Quartz入门案例、核心概念、cron 表达式的格式(了解)
1.quartz入门案例 本入门案例基于spring和quartz整合完成. 第一步:创建maven工程,导入spring和quartz相关依赖 第二步:创建任务类 第三步:在spring配置文件中配 ...
- 深度揭秘腾讯云新一代企业级HTAP数据库TBase核心概念
腾讯云PostgreSQL-XZ(PGXZ)经过公司内部多年业务的打磨,在2017年改名为TBase后,正式对外推出,目前已在政务.医疗.公安.消防.电信.金融等行业等行业的解决方案中大量应用.TBa ...
- kubernetes核心概念
摘抄自: https://www.cnblogs.com/zhenyuyaodidiao/p/6500720.html 1.基础架构 1.1 Master Master节点上面主要由四个模块组成:A ...
随机推荐
- 手机APP开发:学JAVA转安卓APP开发是不是很容易?
成都亿合云商小编为您分享:Android开发是以Java语言为基础的,Android 虽然使用Java 语言作为开发工具,但是在实际开发中发现,还是与Java SDK 有一些不同的地方.Android ...
- R12.2 URL Validation failed. The error could have been caused through the use of the browser's navigation buttons
EBS升级到R12.2.4后,进入系统操作老是报以下错误: 通过谷歌发现有人遇到相同的问题,并提供了解决方案. 原文地址:http://onlineappsdbaoracle.blogspot.com ...
- 使用JFinal的第一个项目出现的问题(The return type is incompatible with JspSourceDependent.getDependants())
四月 08, 2016 4:35:34 下午 org.apache.catalina.core.ApplicationDispatcher invoke严重: Servlet.service() fo ...
- PL/SQL Developer安装详解(32位客户端免安装版)
PL/SQL Developer是一个集成开发环境,专门开发面向Oracle数据库的应用.PL/SQL也是一种程序语言,叫做过程化SQL语言(Procedural Language/SQL).PL/S ...
- .net动态类型在处理json数据方面的应用
我们在.net中处理json数据时,经常需要定义相应的类.比如json数据:{ "name" : "hello", "age" : 18 } ...
- 整理了一下eclipse 快捷键注释的一份文档
// 文档名称 codetemplates.xml <?xml version="1.0" encoding="UTF-8" standalone=&qu ...
- 利用 Serial Over Lan(SOL)搭建 XEN 的调试信息输出环境
如有转载,请注明出处与本文连接,谢谢! 修改XEN的源码实现额外的功能,需要有一个调试环境来得到XEN的调试信息(有关源码编译并安装 XEN 请阅读我以前的博文:在CentOS下源码安装 Xen并搭建 ...
- Java连接SqlServer2008数据库(转)
Java连接SqlServer2008数据库 首先下载JDBC:下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=21599 下 ...
- mysql登陆问题
1.centos6下mysql5.7的登陆问题 问题的描述:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using p ...
- html table表头斜线
关于htnl的table的表头斜线,符合表格设计规范,<style> .biaotou { line-height: 5px; text-align: left; } .biaotou { ...