Lua封装增加CCLabel接口

本文介绍如何在特定游戏开发场景中,利用CCLabel接口实现文本标签的动态添加与Tag替换,包括实例代码演示及关键参数说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加CCLabel接口,支持Tag替换

function AddLabel( str, pos, target, tag, font, anchor, stroked, fz )
    if( font == nil ) then
        font = G_Font_White
    end
    if( anchor == nil ) then anchor = ccp( 0.5, 0.5 ); end
    if( tag == nil ) then tag = 0; end
    if( tag ~= 0 and target:getChildByTag(tag) ) then
        target:removeChildByTag( tag, true );
    end
    local s, e = string.find(font.name, ".fnt")
    local label
    local fontSize = fz
    if fontSize == nil then
        fontSize = font.size
    end
    if s then
        label = CCLabelBMFont:create( str, font.name )
        label:setScale(fontSize)
    else
        label = CCLabelTTF:create( str, font.name, fontSize )
        if stroked or font.stroked then
            AddStrokeOnLabel( label )
        end
    end

    label:setPosition( pos )
    label:setAnchorPoint( anchor )
    label:setColor( font.color )
    target:addChild( label, 0, tag )

    return label;
end

font参数类型如下:

G_Font_YellowStroke_Small = {
    name = "DFPYuanW7-GB.ttf", 
    size = 12,
    color = ccc3(251,224,114),
    stroked = true
}

EX:

AddLabel( "文字", ccp(10,12), father, 0, G_Font_YellowStroke_Small, ccp(0.5, 0.5))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值