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;
};
發佈了124 篇原創文章 · 獲贊 73 · 訪問量 78萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章