加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 16:55:26 所属栏目:百科 来源:网络整理
导读:如果文本的宽度 小于 给出的限定宽度,则不滚动直接返回Label 否则使用ClippingNode 实现在指定宽度与高度的矩形内滚动Label. 返回值是Node 直接 addChild使用即可 // 自动根据文本的宽度稳定滚动速率 text .width/width *5 /** * 创建滚动字幕 * @param txt

如果文本的宽度 小于 给出的限定宽度,则不滚动直接返回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;
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读