cocos js 实现 滚动字幕 且自动根据文本的宽度稳定滚动速率

如果文本的宽度 小于 给出的限定宽度,则不滚动直接返回Label
否则使用ClippingNode 实现在指定宽度与高度的矩形内滚动Label.
返回值是Node 直接 addChild使用即可

// 自动根据文本的宽度稳定滚动速率
text.width/width*5
/**
 *  创建滚动字幕
 * @param txt
 * @param fontsize
 * @param {cc.Color|null} color
 * @param width
 * @param height
 * @returns {cc.Node|*}
 */
createClipRoundText = function(txt,fontsize,color,width,height){
    var text = new cc.LabelTTF(txt,"Arial",fontsize);
    console.log('text width:'+text.width);
    text.setColor(color?color:cc.color.BLUE);
    text.anchorX = 0;
    if(text.width<=width){
        text.anchorY = 0;
        return text;
    }
    var cliper = new cc.ClippingNode();
    var drawNode = new cc.DrawNode();
    drawNode.drawRect(cc.p(0,0),cc.p(width,height),cc.color.WHITE);
    cliper.setStencil(drawNode);
    cliper.anchorX = 0.5;
    cliper.anchorY = 0.5;
    text.y = height/2;
    cliper.addChild(text);
    text.x = width+fontsize;
    text.runAction(cc.repeatForever(cc.sequence(
        cc.moveTo(text.width/width*5,cc.p(-text.width,text.y)),
        cc.callFunc(function(){
            text.x = width+fontsize;
        }))));
    return cliper;
};
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值