LayaAir之骨骼动画(基础)
admin
2023-02-17 00:20:02
0

LayaAir可以是用DragonBone和Spine生成的骨骼动画文件,但是需要将他们的动画文件进行转化,转化后的文件才能够被LayaAir识别.而无论是DragonBone还是Spine都不是LayaAir官方工具,转化的安全和兼容性有些问题,这是一个坑.
到目前为止此转化有2个问题:
①对版本的支持 , 存在迟滞性
②只支持图集模式
无论怎么样 , 总算是部分支持 . 现在先以DragonBone为例讲解骨骼动画.
Ⅰ, DragonBone骨骼动画 , 以Rooster_Ani为例:
LayaAir之骨骼动画(基础)

一 : 开始导出DragonBone骨骼动画文件:

①,导出DB文件 , 由于我的LayaAir , DB转换工具支持DB版本范围是 4.5~5.1.0 , 所以:
LayaAir之骨骼动画(基础)
②,DB导出的骨骼文件有三个 : ske 骨骼文件 , tex.json 纹理文件 , tex.png 纹理图片
LayaAir之骨骼动画(基础)

二:使用LayaAir转换DragonBone骨骼动画格式

①,打开龙骨导出工具 , 进行导出(注意,源文件:DragonBone文件夹上;LayaAir转换的文件夹下)
LayaAir之骨骼动画(基础)
②,将导出的文件(2个),导入到项目中,注意放在bin中:
LayaAir之骨骼动画(基础)

三:代码

①,基本核心
private rooster_dragonbone : Laya.Skeleton = null;

        //显示DragonBone骨骼动画
        this.initDragonAni( true , "rooster_eat_anim");
     /**
     * 初始化DragonBone骨骼动画
     * @param {boolean} $autoPlay 是否自动播放
     * @param {string} $name 动画的名称
     */
    private initDragonAni( $autoPlay : boolean ,  $name : string ) : void{
        this.rooster_dragonbone = new Laya.Skeleton();
        Laya.stage.addChild( this.rooster_dragonbone );
        this.rooster_dragonbone.pos( 410 , 600 );
        this.rooster_dragonbone.load("dragonbone/rooster/Rooster_Ani.sk" , Laya.Handler.create( this, () : void => {
            this.mouse2DragonBone( true );
            if(!$autoPlay || !$name){
                this.rooster_dragonbone.stop();
            }else{
                this.showDragonAni( $name );
            }
        } ));
    }

    private mouse2DragonBone( $isAdd : boolean ) : void {
        if( this.rooster_dragonbone ){
            console.log("ccccccc");
            if($isAdd){
                let $event : Laya.Event = new Laya.Event();
                $event.type = Laya.Event.COMPLETE;
                this.rooster_dragonbone.player.on( Laya.Event.COMPLETE , this , this.onDragonBoneHandler , [$event]);
            }else{
                this.rooster_dragonbone.player.off( Laya.Event.COMPLETE , this , this.onDragonBoneHandler  );
            }
        }
    }

        private onDragonBoneHandler( $e : Laya.Event ) : void{
        console.log("ok");
        switch($e.type){
            case Laya.Event.COMPLETE:
            console.log(`DragonBone 动画播放完毕!!!`);
            break;
        }
    }

    private showDragonAni( $name : string ) : void{
        if( this.rooster_dragonbone && $name){
            this.rooster_dragonbone.play( $name , true );
        }
    }

注意:
1,骨骼动画一定要循环播放才会触发Complete事件.有点坑......
2,如果不调用this.rooster_dragonbone.stop();则播放默认动作:
LayaAir之骨骼动画(基础)

结果:
LayaAir之骨骼动画(基础)


②,扩展鼠标事件
在LayaAir中为2D骨骼动画加mouse响应是复杂的.下边慢慢介绍:
a,寻找响应区域:

        this.rooster_dragonbone.load("dragonbone/rooster/Rooster_Ani.sk" , Laya.Handler.create( this, () : void => {
            let bound : Laya.Rectangle = this.rooster_dragonbone.getBounds(); // 加载完毕之后才能拿到有效的bounds
            let W = bound.width, H = bound.height;
            this.rooster_dragonbone.width = W;   // 若不设置的话, this.rooster_dragonbone.width与node.height均为0
            this.rooster_dragonbone.height = H;
            this.rooster_dragonbone.graphics.drawRect(0, 0, bound.width, bound.height, "#ff00ee");
            this.mouse2DragonBone( true );
            if(!$autoPlay || !$name){
                this.rooster_dragonbone.stop();
            }else{
                this.showDragonAni( $name );
            }
        } ));

注意:让动画停止,方可获取Laya.Rectangle,运行结果如下:
LayaAir之骨骼动画(基础)
这个和DragonBone的关系对应(右下区域块):
LayaAir之骨骼动画(基础)

b,解决方案
因为不可能调整骨骼动画的Laya.Rectangle , 所以需要为骨骼动画套一层(父层),用父层来承担响应.
private rooster_father_dragonbone : Laya.Sprite = null;
//显示DragonBone骨骼动画
this.initDragonAni( true , "rooster_eat_anim");

    /**
     * 初始化DragonBone骨骼动画
     * @param {boolean} $autoPlay 是否自动播放
     * @param {string} $name 动画的名称
     */
    private initDragonAni( $autoPlay : boolean ,  $name : string ) : void{
        this.rooster_father_dragonbone = new Laya.Sprite();
        this.rooster_father_dragonbone.mouseEnabled = this.rooster_father_dragonbone.mouseThrough = true;
        this.rooster_dragonbone = new Laya.Skeleton();
        this.rooster_father_dragonbone.addChild( this.rooster_dragonbone );
        Laya.stage.addChild( this.rooster_father_dragonbone );
        this.rooster_father_dragonbone.pos( 100 , 600 );
        this.rooster_dragonbone.load("dragonbone/rooster/Rooster_Ani.sk" , Laya.Handler.create( this, () : void => {
            let bound : Laya.Rectangle = this.rooster_dragonbone.getBounds(); // 加载完毕之后才能拿到有效的bounds
            let W = bound.width, H = bound.height;
            this.rooster_dragonbone.width = W;   // 若不设置的话, this.rooster_dragonbone.width与node.height均为0
            this.rooster_dragonbone.height = H;
            this.rooster_dragonbone.pos(W/2, H/2); // 骨骼节点偏移(W/2,H/2),使动画区域的左上角与proxy节点的位置(左上角)重合
            this.rooster_father_dragonbone.width = W;
            this.rooster_father_dragonbone.height = H;
            this.rooster_father_dragonbone.graphics.drawRect(0, 0, bound.width, bound.height, "#ff00ee");
            this.mouse2DragonBone( true );
            if(!$autoPlay || !$name){
                this.rooster_dragonbone.stop();
            }else{
                this.showDragonAni( $name );
            }
        } ));
    }

    private mouse2DragonBone( $isAdd : boolean ) : void {
        if( this.rooster_dragonbone ){
            console.log("ccccccc");
            if($isAdd){
                let $event : Laya.Event = new Laya.Event();
                $event.type = Laya.Event.COMPLETE;
                this.rooster_dragonbone.player.on( Laya.Event.COMPLETE , this , this.onDragonBoneHandler , [$event]);
                $event = new Laya.Event();
                $event.type = Laya.Event.MOUSE_DOWN;
                this.rooster_father_dragonbone.on( Laya.Event.MOUSE_DOWN , this , this.onDragonBoneHandler , [$event] );
            }else{
                this.rooster_dragonbone.player.off( Laya.Event.COMPLETE , this , this.onDragonBoneHandler  );
                this.rooster_father_dragonbone.off( Laya.Event.MOUSE_DOWN , this , this.onDragonBoneHandler  );
            }
        }
    }

    private onDragonBoneHandler( $e : Laya.Event ) : void{
        console.log("ok");
        switch($e.type){
            case Laya.Event.COMPLETE:
            console.log(`DragonBone 动画播放完毕!!!`);
            break;
            case Laya.Event.MOUSE_DOWN:
            console.log(`DragonBone 动画被单击`);
            break;
        }
    }

    private showDragonAni( $name : string ) : void{
        if( this.rooster_dragonbone && $name){
            this.rooster_dragonbone.play( $name , true );
        }
    }

显示:
LayaAir之骨骼动画(基础)

控制台打印:
LayaAir之骨骼动画(基础)

相关内容

热门资讯

伊朗总统办公室主任:总统与革命... 新华社德黑兰5月5日电(记者陈霄 沙达提) 据伊朗塔斯尼姆通讯社5日报道,伊朗总统办公室主任哈吉·米...
人民日报:任何企图否定东京审判... 原标题:捍卫历史正义 维护国际秩序——写在东京审判开庭80周年之际钟 佳 黄惠康 《人民日报》(20...
中学生进大厂体验:1个月拿了1... 中学生进大厂体验:1个月拿了1.5万  【中学生进大厂体验:1个月拿了1.5万】2024年年初,19...
多位省委书记省长会见王传福 从... 多位省委书记省长会见王传福  【多位省委书记省长会见王传福】4月27日,河南省委书记刘宁在郑州会见比...
上海汇正财经服务费能退吗?震荡...   部分投资者关心上海汇正财经服务费能退吗,但在二季度A股震荡加剧、地缘与油价双重扰动的复杂环境下,...
市场稀缺权益来袭!从全球急难救...   现在信用卡的功能越来越细化,也越来越人性化了,比如平安银行信用卡新推出的以“一生守护 十分平安”...
美官员:美商船穿越霍尔木兹海峡... 当地时间5月5日,央视记者获悉,两艘搭载美军安全队员的美国商船在通过霍尔木兹海峡期间曾遭伊朗袭击。美...
日本参议员:对俄制裁损害日本国... 正在俄罗斯访问的日本国会参议员铃木宗男5月5日对媒体表示,日本对俄制裁同样损害了日本国家利益。铃木说...
美国务卿称美国正推进对伊朗“极... △美国国务卿鲁比奥(资料图)当地时间5月5日,美国国务卿鲁比奥在媒体简报会上称,美军正在霍尔木兹海峡...
伊朗外交部:敦促美方在外交问题... △伊朗外交部发言人巴加埃(资料图)据伊朗方面5月5日消息,伊朗外交部发言人巴加埃就当前伊美谈判进程表...