龍骨動(dòng)畫換膚

注意事項(xiàng)

替換的圖片需要裁減掉周圍填充的透明像素,因?yàn)辇埞莾?nèi)是以圖片左下角為原點(diǎn)
圖集暫時(shí)還沒測(cè)試,單張圖片已測(cè)

    /**
     * 靜態(tài)圖片修改插槽內(nèi)圖片
     * @param arm 龍骨動(dòng)畫數(shù)據(jù),通過 dragon.armature()獲得
     * @param slotName 插槽名稱 , 龍骨動(dòng)畫里插槽的名字
     * @param sprite 需要更換的圖片 : 此圖片需要消除周圍的透明像素填充 (圖集會(huì)自動(dòng)消除,如果是單個(gè)圖片需要處理)
     * @param isAtlas 是否是圖集
     * @param slotIndex 插槽的displayIndex
     */
    public static changeLocalSpriteFrame(arm : dragonBones.Armature,slotName: string, sprite : cc.SpriteFrame,isAtlas:boolean = false, slotIndex = 0):void{
        let slot = arm.getSlot(slotName);

        let newHeight = isAtlas? sprite._rect.height : sprite.getTexture().height;       
        let newWidth = isAtlas? sprite._rect.width : sprite.getTexture().width;

        let imageData = arm._skinData.displays[slotName][slotIndex];
        let oldRegion = imageData.texture.region;
        //修改圖片的裁剪區(qū)域
        let reg = dragonBones.TextureData.createRectangle();
        if (isAtlas){
            let rect = sprite._rect;
            reg.x = rect.x;
            reg.y = rect.y;
            reg.height = rect.height;         
            reg.width = rect.width;    
        }
        else{
            reg.x = 0;
            reg.y = 0;
            reg.height = newHeight;         
            reg.width = newWidth;   

        }
        imageData.texture.region = reg;
        imageData.texture.spriteFrame = sprite;
        //修改圖片的位置  
        imageData.transform.x -= (newWidth-oldRegion.width)/2;           //這里與x軸正對(duì)
        imageData.transform.y -= (newHeight-oldRegion.height)/2 ;          //與 y 軸相反  
        //將骨骼動(dòng)畫實(shí)時(shí)刷新
        slot.display._updateDisplayData()
        slot._transformDirty = true;
        slot._displayDirty = true;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容