Egret之龙骨卡槽(slot)换皮
admin
2023-02-17 14:00:09
0

龙骨的图片是绑定在卡槽上的.并且是一对一的关系.所以可以通过对骨架的卡槽上绑定的图片的更换来实现另一种换皮的效果.

换皮的核心代码:

    //针对slot设置其新内容
    private setNewSlot( slotName:string, textureName:string )
    {
        //方法1
        var slot:dragonBones.Slot = this._armature.getSlot( slotName );
        var b:egret.Bitmap = new egret.Bitmap();
        b.texture = RES.getRes( textureName );
        b.x = slot.display.x;
        b.y = slot.display.y;
        b.anchorOffsetX = b.width/2;
        b.anchorOffsetY = b.height/2;
        slot.setDisplay( b );

        //方法2,仅限于slot中内容为Bitmap
        //var slot:dragonBones.Slot = this._armature.getSlot(slotName);
        //slot.display.texture = RES.getRes(textureName);
    }

我做了一个工具:

module bg2tool{
    /**
     * 龙骨动画工具--单例
     * @author Husz
     */
    export class DragonBoneAnimationTools{
        private static _instance : DragonBoneAnimationTools = null;

        /**
         * 获取单例(只读......)
         * @returns {bg2tool.DragonBoneAnimationTools}
         * @constructor
         */
        public static get Instance() : DragonBoneAnimationTools{
            if( DragonBoneAnimationTools._instance == null ){
                DragonBoneAnimationTools._instance = new DragonBoneAnimationTools();
            }
            return DragonBoneAnimationTools._instance;
        }

        /**
         * 防止类外实例化
         */
        private constructor(){}

        /**
         * 设置骨骼卡槽的皮肤
         * @param {dragonBones.EgretArmatureDisplay} $animation 骨骼动画
         * @param {string} $slotName 卡槽名称
         * @param {string} $textureName 纹理
         */
        public setNewSlot( $animation : dragonBones.EgretArmatureDisplay ,  $slotName:string, $textureName:string ) : void{
            if( DEBUG ){
                if( !RES.hasRes($textureName) ){
                    egret.error(`骨骼动画卡槽换装 ${$textureName} 纹理没有配置!!!`);
                }else{
                    if( RES.getRes( $textureName ) == null ){
                        egret.error(`骨骼动画卡槽换装 ${$textureName} 纹理没有被加载!!!`);
                    }
                }
            }
            let slot:dragonBones.Slot = $animation.armature.getSlot($slotName);
            let b:egret.Bitmap = new egret.Bitmap();
            b.texture = RES.getRes( $textureName );
            b.x = slot.display.x;
            b.y = slot.display.y;
            b.anchorOffsetX = b.width>>1;
            b.anchorOffsetY = b.height>>1;
            slot.display = b;
        }
    }
}

现在准备测试

①: 准备龙骨资源
Egret之龙骨卡槽(slot)换皮

注意:
球的图片资源绑定的卡槽名称为 slot
Egret之龙骨卡槽(slot)换皮

看看初始骨骼动画效果:

private $dragonBone : dragonBones.EgretArmatureDisplay = null;
                this.$dragonBone = bg2tool.DragonBonePoolResManager.Instance.getAnimation4DragonBone(
                    "GuajiSelfBone_ske_json",
                    "GuajiSelfBone_tex_json",
                    "GuajiSelfBone_tex_png",
                    "guajiAnimation",
                    1,
                    1
                );
                this._context.GuajiAnimation.addChild( this.$dragonBone );

                this.$dragonBone.x = 200;
                this.$dragonBone.y = 400;

                this.$dragonBone.animation.play("jumping",0);

结果:
Egret之龙骨卡槽(slot)换皮

换皮开始:
换成:
Egret之龙骨卡槽(slot)换皮

代码:

                this.$dragonBone = bg2tool.DragonBonePoolResManager.Instance.getAnimation4DragonBone(
                    "GuajiSelfBone_ske_json",
                    "GuajiSelfBone_tex_json",
                    "GuajiSelfBone_tex_png",
                    "guajiAnimation",
                    1,
                    1
                );
                this._context.GuajiAnimation.addChild( this.$dragonBone );

                this.$dragonBone.x = 200;
                this.$dragonBone.y = 400;

                this.$dragonBone.animation.play("jumping",0);

                bg2tool.ResSyncLoadingManager.Instance.startLoading( "1-1_99003_png" , this.changeSkin.bind(this) );

                        private changeSkin( $skin : string ) : void{
                                    if( $skin == "1-1_99003_png" ){
                                            bg2tool.DragonBoneAnimationTools.Instance.setNewSlot( this.$dragonBone , "slot" , $skin );
                                    }
        }

结果 :
Egret之龙骨卡槽(slot)换皮

相关内容

热门资讯

菲律宾,没有以色列的命,得了以... 菲律宾,马尼拉。美国国务卿鲁比奥来了。菲律宾期盼已久的“救星”来了。菲律宾认为的“阳光”来了。从机场...
服务器盛会东莞启幕,松山湖软硬... 7月23日至24日,“智算筑基 融链共赢——2026年服务器产业供需对接活动”在东莞举行。作为系列配...
面向6G通信、高端安检、精准医... 太赫兹作为“电磁波段最后一块未开垦之地”,近年在超快光电子、低尺度半导体技术的推动下,逐步从实验室走...
获得全球最高奖项的中国数学家,... 一个喜欢玩梗的二次元大佬,竟然站上了数学界最高荣誉的领奖台。他就是今年的菲尔兹奖得主之一,邓煜。另一...
单向空间杭州旗舰店8年后谢幕,... 去年12月,单向空间杭州乐堤港店员工回应闭店谣言,称“没有收到任何闭店通知”。不到一年,停止营业的消...
带娃家长刷不到下铺,算法不该把... 据新华社的报道,一位宝妈带4岁娃要从昭通去杭州南,在12306上购票时反复被分到上铺,一天内取消三次...
安徽一高标准农田电机出水异常,... 近日,安徽省淮北市濉溪县百善镇丁楼村村民向《都市现场》爆料称:村里2023年建成的高标准农田配套电机...
河北一演唱会音响“翻车”,人声... 近日,来自北京的庞女士向都市现场记者反映,她7月11日在河北唐山观看“青春超燃巨星演唱会”时遭遇了极...
尺素金声|中国经济“失速论”站... 一季度GDP同比增长5.0%、二季度增长4.3%、上半年增长4.7%,2026年中国经济半年报发布后...
NVIDIA最深的护城河要凉!... 7月24日消息,AMD在Advancing AI预简报会上表示,CUDA已不再是外界想象中的那条护城...