[LayaNative 2.0]同步龙骨插槽动画方案

Laya Ver2.1.1
 
export class MSketon { 
    private mFactory: Laya.Templet;
    public mArmature: Laya.Skeleton
    private _handler: Laya.Handler; 
    constructor(url: string, handler: Laya.Handler, aniMode: number = 0) {
        this._handler = handler;
        this.mFactory = new Laya.Templet();
        this.mFactory.on(Laya.Event.COMPLETE, this, this.parseComplete, [aniMode]);
        this.mFactory.loadAni(url);
    } 
    private parseComplete(aniMode: number, fac: Laya.Templet): void {
        this.mArmature = this.mFactory.buildArmature(aniMode);
        this._handler.runWith(this.mArmature);
    } 
    /** 获取骨骼矩阵 */
    public getMatrix(slotName: string): laya.maths.Matrix {
        return this.mArmature.getSlotByName(slotName).getMatrix();
    } 
    public destroy() {
        this.mArmature.stop();//停止龙骨动画播放
        this.mFactory.off(Laya.Event.COMPLETE, this, this.parseComplete);
        this.mArmature.removeSelf();//从显示列表移除龙骨动画本身
        this.mArmature.removeChildren();//从显示列表移除龙骨动画子对象
        if (!this.mArmature.destroyed) {
            this.mArmature.destroy(true);
        }
        this.mFactory.destroy();//释放动画模板类下的纹理数据
    }
}
 
 
xx{
    this.mSketon = new MSketon(this.type, Laya.Handler.create(this, this.onSkeLoaded), 2);
    Laya.timer.frameLoop(1, this, this.update);
.......................................................................................................................................................................................
    private update() {
        if (this.mSketon.mArmature) {
            let matrix: laya.maths.Matrix = this.mSketon.getMatrix(this.getSlotNmae());
            if (matrix) {
                this.img.scaleX = matrix.a;
                this.img.scaleY = matrix.d;
                this.img.x = matrix.tx;
                this.img.y = matrix.ty;
                this.img.rotation = matrix.b * 180 / Math.PI;
            }
        }
    }
}
 
注意事项:
/**
         * 创建动画
         * 0,使用模板缓冲的数据,模板缓冲的数据,不允许修改                    (内存开销小,计算开销小,不支持换装)
         * 1,使用动画自己的缓冲区,每个动画都会有自己的缓冲区,相当耗费内存    (内存开销大,计算开销小,支持换装)
         * 2,使用动态方式,去实时去画                                       (内存开销小,计算开销大,支持换装,不建议使用)
         * @param aniMode 0 动画模式,0:不支持换装,1,2支持换装
         * @return 
         */
        buildArmature(aniMode?:number):laya.ani.bone.Skeleton;
 
aniMode: 要传2,否则只会在加载后的第一次播放生效 

插槽具体参数请参考Matrix的注释
已邀请:

要回复问题请先

商务合作
商务合作