【Cocos2dx 3.3 Lua】滚动字幕
1、原理
如下图
由底层和字幕以及遮盖层组成
水平滚动:
|
RollingFont=class("RollingFont",function()
return cc.Layer:create()
end)
RollingFont.ctor=function(self)
local cache=cc.SpriteFrameCache:getInstance()
cache:addSpriteFrames("rollfont/ui_serverlist.plist")
self.size=cc.Director:getInstance():getWinSize()
--注册定时事件
self:registerScriptHandler(function(tag)
local scheduler=nil
if tag=="enter" then
scheduler=self:onEnter()
elseif tag=="exit" then
self:onExit(scheduler)
end
end)
end
RollingFont.createText=function(self,text)
local ttfConfig = {}
ttfConfig.fontFilePath="fonts/arial.ttf"
ttfConfig.fontSize=11.4
local label = cc.Label:createWithTTF(ttfConfig, text, cc.TEXT_ALIGNMENT_LEFT, self.size.width)
label:setTextColor( cc.c4b(, , , ) )
return label
end
--垂直滚动字幕
RollingFont.verticalFont=function(self)
--添加垂直滚动字幕边框
local listbox=cc.Sprite:createWithSpriteFrameName("login_listbase.png")
listbox:setPosition(cc.p(self.size.width/,self.size.height/+))
listbox:setScale(1.2)
self:addChild(listbox)
local listboxSize=listbox:getBoundingBox()
local listboxX=listbox:getPositionX()
local listboxY=listbox:getPositionY()
self.verticalRect=cc.rect(listboxX-listboxSize.width/,listboxY-listboxSize.height/,listboxSize.width,listboxSize.height)
--垂直滚动字幕
local text="1.Hi! Welcome to My Blog,This is a Sample about\nfont vertical move with Cocos2dx 3.x Lua\n"
local label=self:createText(text)
label:setPosition(self.verticalRect.x+self.verticalRect.width-,self.verticalRect.y)
label:setAnchorPoint(,)
self:addChild(label)
self.labelVertical=label
--字幕遮罩
local fg=cc.Sprite:create("rollfont/fg.png")
fg:setPosition(cc.p(self.verticalRect.x+self.verticalRect.width/,self.verticalRect.y+self.verticalRect.height/));
self:addChild(fg);
end
--水平滚动字幕
RollingFont.horizontalFont=function(self)
--添加水平滚动字幕边框
local listbox=cc.Sprite:createWithSpriteFrameName("login_textbase.png")
listbox:setPosition(cc.p(self.size.width/,self.size.height/-))
listbox:setScaleX(2.5)
self:addChild(listbox)
local listboxSize=listbox:getBoundingBox()
local listboxX=listbox:getPositionX()
local listboxY=listbox:getPositionY()
--水平滚动字幕
local text="1.Hi! This is a Sample about font vertical move with Cocos2dx 3.x Lua"
local label=self:createText(text)
label:setPosition(self.size.width,self.verticalRect.y-)
label:setAnchorPoint(,)
self:addChild(label)
self.labelHorizontal=label
local labelSize=label:getBoundingBox()
self.horizontalSize=cc.rect(,,labelSize.width,labelSize.height)
end
RollingFont.onEnter=function(self)
--垂直滚动最大和最小高度
local hlabelSize=self.labelVertical:getBoundingBox()
local hmaxHeight=self.verticalRect.y+self.verticalRect.height+hlabelSize.height
local hminHeight=self.verticalRect.y+
--水平滚动最大宽度和最小宽度
local vmaxWidth=self.size.width
local vminWidth=-self.horizontalSize.width
local function schedule()
--垂直滚动
local y=self.labelVertical:getPositionY()
y=y+
if y >= hmaxHeight then
y=hminHeight
end
self.labelVertical:setPositionY(y)
--水平滚动
local x=self.labelHorizontal:getPositionX()
x=x-
if x <= vminWidth then
x=vmaxWidth
end
self.labelHorizontal:setPositionX(x)
end
local scheduler=cc.Director:getInstance():getScheduler()
scheduler:scheduleScriptFunc(schedule,0.02,false)
return scheduler
end
RollingFont.onExit=function(self,scheduler)
if scheduler then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(scheduler)
end
end
RollingFont.create=function(self)
local roll=RollingFont.new()
roll:verticalFont()
roll:horizontalFont()
return roll
end
return RollingFont
3、执行效果
【Cocos2dx 3.3 Lua】滚动字幕的更多相关文章
- 【Cocos2dx 3.x Lua】TileMap使用
1.编辑TileMap地图资源 2.Cocos2dx 3.x Lua中使用TileMap Link: http://codepad.org/P0nFP1Dx local TileMap=clas ...
- HTML滚动字幕代码参数详解及Js间隔滚动代码
html文字滚动代码 <marquee style="WIDTH: 388px; HEIGHT: 200px" scrollamount="2" dire ...
- UILabel滚动字幕的实现
经常需要在应用中显示一段很长的文字,比如天气或者广告等,这时候使用滚动字幕的方式比较方便. 参考文献: [1] YouXianMing, 使用UILabel实现滚动字幕移动效果, 博客园 [2] ht ...
- js原生 + jQuery实现页面滚动字幕
js原生/jQuery实现页面滚动字幕效果 17:45:49 在新闻列表或者文章列表信息等页面中很容易要求实现字幕滚动的效果,以下为简单的实现页面中滚动字幕的效果 1.jQuery实现页面滚动字幕效果 ...
- C#-循环滚动字幕,timer,从左至右,从右至左,暂停---ShinePans
Lable的Left属性是能够更改的,可是 Right属性不能够更改,所以我们能够利用 这个特点做自加 自减运算 using System; using System.Collections.Gene ...
- Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试)
Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试) 本篇博客介绍Cocos2d-x 3.2Lua演示样例中点击移动的样例,在这个样例你能够得到怎样创建单点触 ...
- Cocos2d-x 脚本语言Lua中的面向对象
Cocos2d-x 脚本语言Lua中的面向对象 面向对象不是针对某一门语言,而是一种思想.在面向过程的语言也能够使用面向对象的思想来进行编程. 在Lua中,并没有面向对象的概念存在,没有类的定义和子类 ...
- Cocos2d-x 3.2 Lua演示样例 XMLHttpRequestTest(Http网络请求)
Cocos2d-x 3.2 Lua演示样例 XMLHttpRequestTest(Http网络请求) 本篇博客介绍Cocos2d-x 3.2Lua演示样例中的XMLHttpRequestTes ...
- Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试)
Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试) 本篇博客介绍Cocos2d-x 3.2中Lua演示样例的音频測试.Cocos2d-x使用SimpleAudi ...
随机推荐
- linux多行注释
1.多行注释: 1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式; 2. 在行首使用上下键选择需要注释的多行; 3. 按下键盘(大写)“I”键,进入插入模式 ...
- Android Studio 3.0.1 版本包下载
Android Studio 3.0.1 发布了,这是对 Android Studio 3.0 的一个小的更新,包括一般错误修复和性能改进 下载地址: Windows 64 位:https://dl. ...
- vscode 使用sublime风格代码
Monokai 主题 One Dark Pro主题 vscode设置字体 "editor.fontFamily": "MONACO, Consolas, 'Couri ...
- Packetbeat协议扩展开发教程(1)
Packetbeat ( https://www.elastic.co/products/beats/packetbeat )是一个开源的网络抓包与分析框架,内置了很多常见的协议解析,如HTPP.My ...
- 万事开头难 && 实践出真知
实践出真知,真是千古不变的真理. 前几天在顺手做一个万年历项目,实现了用TFT屏显示实时时间,日期,温度,和按键设置时间,能在特定时间显示特定的话语在显示屏上面.其实这个项目现在想想还是挺简单的.我的 ...
- SQL语句的执行过程
1.语法校验 如果在SQL计划缓存中没有对应的执行计划,服务器首先会对用户请求的SQL语句进行语法效验,如果有语法错误,服务器会结束查询操作,并用返回相应的错误信息给调用它的应用程序. 注意:此时返回 ...
- confirm
注意confirm是window对象的方法,当确认时,返回true,取消时,返回false
- ubuntu下绝对路径和相对路径的问题
绝对路径与相对路径 除了需要特别注意的FHS目录配置外,在文件名部分我们也要特别注意.因为根据档名写法的不同,也可将所谓的路径(path)定义为绝对路径(absolute)与相对路径(relativ ...
- HDU 6312 - Game - [博弈][杭电2018多校赛2]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 Problem Description Alice and Bob are playing a ...
- Oracle体系结构之联机日志文件管理
日志文件分类:重做日志文件 归档日志文件 警告日志文件 跟踪日志文件 redo_log_file文件作用: 1)维护数据一致性 2)记录了数据库里的更改操作 redo_log_friles以组为单位, ...