DragonBones / DragonBonesJS

DragonBones TypeScript / JavaScript Runtime
MIT License
746 stars 321 forks source link

Phaser 3 - How can I change the slot skin programmatically? #126

Closed scott20145 closed 4 years ago

scott20145 commented 4 years ago

I'd like to know does there have any document or pseudo code to show how to change the skin on the runtime?

Thanks for the help.

The example I found is for Laya:

skeleton=templete.buildArmature(1);//创建动画,类型:1   支持换装
skeleton.pos(150,250);//动画位置
skeleton.play(0,true);//动画播放,从0帧开始,不断循环播放
skeleton.scale(0.5,0.5);//动画缩放为原始状态的二分之一
skeleton.replaceSlotSkinByIndex('head',0,1);//head插槽下,让索引为1的皮肤替换索引为0的皮肤
scott20145 commented 4 years ago

Finally, I found the solution:

const slot = armatureDisplay.armature.getSlot('head');
(armatureDisplay as any).factory.replaceSlotDisplay("Dragon", "Dragon", 'head', 'head2', slot);

But I just can't found BaseFactory, so I export to armatureDisplay object. Has any standard way to access BaseFactory?

jcyuan commented 4 years ago

yes to access the factory instance, you'd better to call this.[dbPluginName].factory (assume that your code is right in the scene class). for example, in your game config you inject the dbplugin with name 'dragonbone':

cfg = { 
....
plugins: {
        scene: [
            { key: "DragonBones", plugin: dragonBones.phaser.plugin.DragonBonesScenePlugin, mapping: "dragonbone" }    // setup DB scene plugin
        ]
    },
....
}
new Phaser.Game(cfg);

and in your scene class, you need to define a type definition to avoid the error 'dragonbone is not defined'. for example

class myScene extends Phaser.Scene {
           dragonbone: dragonBones.phaser.plugin.DragonBonesScenePlugin;

           create(): void {     this.dragonbone.factory....       }
}