[Cocos Creator] 本地文件加载系列六:本地龙骨动画dragonbones加载(web模式)

[Cocos Creator] 本地文件加载系列一:本地文件读取(web模式)

[Cocos Creator] 本地文件加载系列二:本地音频文件播放(web模式)

[Cocos Creator] 本地文件加载系列三:本地粒子文件加载和播放(web模式)

[Cocos Creator] 本地文件加载系列四:本地图片文件加载(web模式)

[Cocos Creator] 本地文件加载系列五:本地spine文件加载(web模式)

 

// TS
// 方法参考前文
// dragonBone: dragonBones.ArmatureDisplay = null;
readLocalFile(dragonbonesFile, READ_FILE_TYPE.TEXT, (animResult: string | ArrayBuffer) => {
    if (animResult && atlasFile) {
        readLocalFile(atlasFile, READ_FILE_TYPE.TEXT, (atlasResult: string | ArrayBuffer) => {
            if (atlasResult && imgFile) {
                readLocalFile(imgFile, READ_FILE_TYPE.DATA_URL, (imgResult: string | ArrayBuffer) => {
                    if (imgResult) {
                        let fileBase64 = imgResult.toString();
                        base64ToTexture2D(fileBase64, (texture: cc.Texture2D) => {
                            let dragonAtlasAsset = new dragonBones.DragonBonesAtlasAsset();
                            dragonAtlasAsset.atlasJson = atlasResult.toString();
                            dragonAtlasAsset.texture = texture;
                            let dragonAsset = new dragonBones.DragonBonesAsset();
                            dragonAsset.dragonBonesJson = animResult.toString();
                                        
                            dragonBone.dragonAsset = dragonAsset;
                            dragonBone.dragonAtlasAsset = dragonAtlasAsset;
                            // 获取所有动画名
                            // let armatureNames = dragonBone.getArmatureNames();
                            let json = JSON.parse(dragonAsset.dragonBonesJson);
                            let armatures = json["armature"];
                            let armatureNames = [];
                            for (let i = 0; i < armatures.length; i++) {
                                armatureNames.push(armatures[i].name);
                            }
                            if (armatureNames.length > 0) {
                                let defaultArmatureName = armatureNames[0];
                                dragonBone.armatureName = defaultArmatureName;
                                // let animationNames = dragonBone.getAnimationNames(defaultArmatureName);
                                let defaultArmature = armatures[0];
                                let animations = defaultArmature.animation;
                                let animationNames = [];
                                for (let i = 0; i < animations.length; i++) {
                                    animationNames.push(animations[i].name);
                                }
                                if (animationNames.length > 0) {
                                    dragonBone.playAnimation(animationNames[0], 0);
                                }
                            }
                        });
                    }
                });
            }
        });
    }
});

不清楚的地方查看引擎代码

 

你可能感兴趣的:(TS,js,cocoscreator)