Cocos2dx+lua中Color參數的坑


cocos2x的lua中有如下幾種顏色定義

--Color3B
function cc.c3b( _r,_g,_b )
return { r = _r, g = _g, b = _b }
end

--Color4B
function cc.c4b( _r,_g,_b,_a )
return { r = _r, g = _g, b = _b, a = _a }
end

--Color4F
function cc.c4f( _r,_g,_b,_a )
return { r = _r, g = _g, b = _b, a = _a }
end

今天使用Cocos2dx的Label碰到一個問題,我感覺挺容易被忽略的

virtual void enableShadow(const Color4B& shadowColor = Color4B::BLACK,const Size &offset = Size(2,-2), int blurRadius = 0);

使用Label的enableShadow函數定義的顏色要求是Color4B,如果不小心使用了Color3B就根本沒有效果

我看了下從lua表轉化到C++中Color4B的代碼,結果是為空時就賦值為0,所以使用Color3B傳進來,alpha值為nil,它直接賦值為0了

全透明了當然也就看不到了。。。

bool luaval_to_color4b(lua_State* L,int lo,Color4B* outValue)
{
    if (NULL == L || NULL == outValue)
        return false;
    
    bool ok = true;

    tolua_Error tolua_err;
    if (!tolua_istable(L, lo, 0, &tolua_err) )
    {
#if COCOS2D_DEBUG >=1
        luaval_to_native_err(L,"#ferror:",&tolua_err);
#endif
        ok = false;
    }
    
    if(ok)
    {
        lua_pushstring(L, "r");
        lua_gettable(L,lo);
        outValue->r = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1);
        lua_pop(L,1);
        
        lua_pushstring(L, "g");
        lua_gettable(L,lo);
        outValue->g = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1);
        lua_pop(L,1);
        
        lua_pushstring(L, "b");
        lua_gettable(L,lo);
        outValue->b = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1);
        lua_pop(L,1);
        
        lua_pushstring(L, "a");
        lua_gettable(L,lo);
        outValue->a = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1);
        lua_pop(L,1);
    }
    
    return ok;
}

還有如果需要傳入Color4F的地方如果傳入了Color3B或者Color4B也會不正常

因為Color4F要求rgba范圍都是0~1

void drawDot(const Vec2 &pos, float radius, const Color4F &color);

lua本來就是弱類型的這樣很容易出錯!!

只能自己多加注意了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM