微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
};

原文地址:https://www.jb51.cc/cocos2dx/340149.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐