参考资料:
 

1、原理

        通过调用update来更新位置达到的移动效果,和背景滚动有点类似,一旦滚动结束就重置为起点,开始新的滚动。只是为了达到在某个区域内滚动而不至于超出这个区域,有时候会用一些前景图来做遮盖,所以这个时候其实字幕或者公告是有在后面滚动的,但是被遮住了,因此看起来就像是只在某个区域内滚动。
 
垂直滚动:

如下图

由底层和字幕以及遮盖层组成

水平滚动:

     设置字幕在update时x轴移动,移动到屏幕外时重新开始移动
 
2、实现
RollingFont.lua

Link: http://codepad.org/i2T4gEvB    [ raw code | fork ]  
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】滚动字幕的更多相关文章

  1. 【Cocos2dx 3.x Lua】TileMap使用

    1.编辑TileMap地图资源 2.Cocos2dx 3.x Lua中使用TileMap   Link: http://codepad.org/P0nFP1Dx  local TileMap=clas ...

  2. HTML滚动字幕代码参数详解及Js间隔滚动代码

    html文字滚动代码 <marquee style="WIDTH: 388px; HEIGHT: 200px" scrollamount="2" dire ...

  3. UILabel滚动字幕的实现

    经常需要在应用中显示一段很长的文字,比如天气或者广告等,这时候使用滚动字幕的方式比较方便. 参考文献: [1] YouXianMing, 使用UILabel实现滚动字幕移动效果, 博客园 [2] ht ...

  4. js原生 + jQuery实现页面滚动字幕

    js原生/jQuery实现页面滚动字幕效果 17:45:49 在新闻列表或者文章列表信息等页面中很容易要求实现字幕滚动的效果,以下为简单的实现页面中滚动字幕的效果 1.jQuery实现页面滚动字幕效果 ...

  5. C#-循环滚动字幕,timer,从左至右,从右至左,暂停---ShinePans

    Lable的Left属性是能够更改的,可是 Right属性不能够更改,所以我们能够利用 这个特点做自加 自减运算 using System; using System.Collections.Gene ...

  6. Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试)

    Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试)  本篇博客介绍Cocos2d-x 3.2Lua演示样例中点击移动的样例,在这个样例你能够得到怎样创建单点触 ...

  7. Cocos2d-x 脚本语言Lua中的面向对象

    Cocos2d-x 脚本语言Lua中的面向对象 面向对象不是针对某一门语言,而是一种思想.在面向过程的语言也能够使用面向对象的思想来进行编程. 在Lua中,并没有面向对象的概念存在,没有类的定义和子类 ...

  8. Cocos2d-x 3.2 Lua演示样例 XMLHttpRequestTest(Http网络请求)

    Cocos2d-x 3.2 Lua演示样例 XMLHttpRequestTest(Http网络请求)     本篇博客介绍Cocos2d-x 3.2Lua演示样例中的XMLHttpRequestTes ...

  9. Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试)

    Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试)  本篇博客介绍Cocos2d-x 3.2中Lua演示样例的音频測试.Cocos2d-x使用SimpleAudi ...

随机推荐

  1. PowerDesigner 同名问题解决 Entity Attribute name uniqueness

    选择"Tools -> Model Options"后 "Allow reuse"复选框,建议把这个钩也去掉 Tool->check model.. ...

  2. VLC媒体视频播放器 v3.0.2官方版

    https://www.videolan.org/    VLC media player http://www.pc6.com/softview/SoftView_52483.html    VLC ...

  3. Telnet是什么意思又是什么协议 Telnet有什么作用及功能

    Telnet是teletype network的缩写,专业的说,Telnet是Internet上远程登录的一种程序:它可以让您的电脑通过网络登录到网络另一端的电脑上,甚至还可以存取那台电脑上的文件. ...

  4. 使用spring提供的ReflectionUtils简化项目中反射代码的复杂性

    在项目中有时候我们会使用到反射的功能,如果使用最原始的方法来开发反射的功能的话肯能会比较复杂,需要处理一大堆异常以及访问权限等问题.spring中提供了ReflectionUtils 这个反射的工具类 ...

  5. C# 根据域名获取IP地址

    今天做海康微视视频接口的时候要用到硬盘录像机的IP地址.端口号.用户名和密码. 但是发现客户IP地址是动态获取的,经常变化. 所以需要根据域名解析出IP. 代码如下 //判断输入的是否是IP Rege ...

  6. 安装 SQL SERVER MsiGetProductInfo 无法检索 Product Code 1605错误 解决方案

    重装数据库服务器上的SQL SERVER 2008 上遇到了以下问题 标题: SQL Server 安装程序失败. SQL Server 安装程序遇到以下错误: MsiGetProductInfo 无 ...

  7. 【CF891E】Lust 生成函数

    [CF891E]Lust 题意:给你一个长度为n的序列$a_i$,对这个序列进行k次操作,每次随机选择一个1到n的数x,令$res+=\prod\limits_{i!=x}a_i$(一开始res=0) ...

  8. 【CF720D】Slalom 扫描线+线段树

    [CF720D]Slalom 题意:一个n*m的网格,其中有k个矩形障碍,保证这些障碍不重叠.问你从(1,1)走到(n,m),每步只能往右或往上走,不经过任何障碍的方案数.两种方案被视为不同,当且仅当 ...

  9. wordpress---page页面数据调用

    在wordpress的开发中,会使用wordpress的的页面,那么页面数据该怎么调用呢? 拿到页面的 content: <?php if(have_posts()) : ?> <? ...

  10. CSS基础问题

    1.css引入问题 本来以为css引入是很简单的问题,但是在写demo中,使用连接方式引入就出现了问题,找了半天,还是说一下问题吧. 在引入时没有写rel="stylesheet" ...