1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| const { dragonBones } = window;
class DragonBonesLoader {
static loadAndPlay(parent, baseUrl, name, armatureName, animName) { const imageUrl = `${baseUrl}/${name}_tex.png`; const atlasUrl = `${baseUrl}/${name}_tex.json`; const skeUrl = `${baseUrl}/${name}_ske.json`;
this.loadResources(imageUrl, atlasUrl, skeUrl) .then(({ texture, atlasJson, skeJson }) => { this.createArmatureDisplay(parent, texture, atlasJson, skeJson, armatureName, animName); }) .catch(error => { console.error('Failed to load dragonbones:', error); }); }
static loadResources(imageUrl, atlasUrl, skeUrl) { return new Promise((resolve, reject) => { cc.loader.load(imageUrl, (err, texture) => { if (err) { reject(new Error(`Failed to load image: ${err}`)); return; }
cc.loader.load({ url: atlasUrl, type: 'txt' }, (err, atlasJson) => { if (err) { reject(new Error(`Failed to load atlas: ${err}`)); return; }
cc.loader.load({ url: skeUrl, type: 'txt' }, (err, skeJson) => { if (err) { reject(new Error(`Failed to load skeleton: ${err}`)); return; }
resolve({ texture: texture, atlasJson: atlasJson, skeJson: skeJson }); }); }); }); }); }
static createArmatureDisplay(parent, texture, atlasJson, skeJson, armatureName, animName) { const animNode = new cc.Node('DragonBonesNode'); animNode.parent = parent;
const dragonDisplay = animNode.addComponent(dragonBones.ArmatureDisplay);
const atlasAsset = new dragonBones.DragonBonesAtlasAsset(); atlasAsset.atlasJson = atlasJson; atlasAsset.texture = texture;
const dbAsset = new dragonBones.DragonBonesAsset(); dbAsset.dragonBonesJson = skeJson;
dragonDisplay.dragonAtlasAsset = atlasAsset; dragonDisplay.dragonAsset = dbAsset;
dragonDisplay.armatureName = armatureName;
dragonDisplay.playAnimation(animName, 0);
return dragonDisplay; } }
window.DragonBonesLoader = DragonBonesLoader;
|