Laya 实操九:资源加载+图片切换+创建预设+打包图集+图集帧动画

资源加载

export default class Test extends Laya.Script {
    constructor() { super(); }
    
    private self: any = null;
 
    onAwake(){
        this.self = this.owner;
    }

    onStart(){
        //普通
        /*
        Laya.loader.load("gameass/BackgroundMusic.wav",Laya.Handler.create(this,function (){console.log("ok");}));
        */

        //含参
       /*
       Laya.loader.load("gameass/BackgroundMusic.wav",Laya.Handler.create(this,function(sound){console.log("ok",sound);}),null,Laya.Loader.SOUND);
       */

        //多个+进度
        /*
        Laya.loader.load(["gameass/BackgroundMusic.wav","gameass/load.jpg"],Laya.Handler.create(this,function(){console.log("ok");}),Laya.Handler.create(this,function(per){console.log(per);}));
        */

        //获取
        /*
        Laya.loader.load(["gameass/BackgroundMusic.wav","gameass/load.jpg"],Laya.Handler.create(this,function(){
        console.log("ok");
        let sound = Laya.loader.getRes("gameass/BackgroundMusic.wav");
        console.log(sound);
        }),Laya.Handler.create(this,function(per){console.log(per);}));
        */

        //清理
        /*
        Laya.loader.load(["gameass/BackgroundMusic.wav","gameass/load.jpg"],Laya.Handler.create(this,function(){
        console.log("ok");
        let sound = Laya.loader.getRes("gameass/BackgroundMusic.wav");
        console.log(sound);
        Laya.loader.clearRes("gameass/BackgroundMusic.wav");
        sound = Laya.loader.getRes("gameass/BackgroundMusic.wav");
        console.log(sound);
        }),Laya.Handler.create(this,function(per){console.log(per);}));
        */

        //分组
        /*
       Laya.loader.load(["gameass/BackgroundMusic.wav","gameass/Man_feiji.ogg","gameass/load.jpg"],Laya.Handler.create(this,function(){
        console.log("ok");
        let sound = Laya.loader.getRes("gameass/BackgroundMusic.wav");
        console.log(sound);

        Laya.loader.setGroup("gameass/BackgroundMusic.wav","sound");
        Laya.loader.setGroup("gameass/Man_feiji.ogg","sound");

        Laya.loader.clearResByGroup("sound");
        sound = Laya.loader.getRes("gameass/BackgroundMusic.wav");
        console.log(sound);

        }),Laya.Handler.create(this,function(per){console.log(per);}));
        */

        //错误处理
        /*
       Laya.loader.load("gameass/BackgroundMusic",Laya.Handler.create(this,function (){console.log("ok");}));

       Laya.loader.on(Laya.Event.ERROR,this,function(err){
           console.log("err:",err);
       })
       */
    }
}

图片切换

export default class Test extends Laya.Script {
    constructor() { super(); }
    
    private self: any = null;
 
    onAwake(){
        this.self = this.owner;
    }

    onStart(){

        //this.self.texture = "gameass/background.png";

        Laya.loader.load("gameass/background.png",Laya.Handler.create(this,function (bg){
            console.log("ok");
            this.self.texture = bg;
        }));

    }
}

创建预设

export default class Test extends Laya.Script {
    constructor() { super(); }

    onStart(){
        Laya.loader.load("gameass/player.prefab",Laya.Handler.create(this,function (player){
            console.log("ok");
            let node = player.create();
            this.owner.addChild(node);
            node.x = 100;
            node.y = 100;
        }));

    }
}

图集

图片属性设为默认,F9打开项目设置/图集设置,进行设置。

F12导出代码,结束后可以在图集设置中的路径下找到图集。

图集帧动画

export default class Test extends Laya.Script {
    constructor() { super(); }

    private self: any = null;
 
    onAwake(){
        this.self = this.owner;
    }

    onStart(){
        Laya.loader.load("res/atlas/gameass/DDZ.atlas",Laya.Handler.create(this,function (at){
            console.log("ok");
            let anim = new Laya.Animation();
            anim.loadAtlas("res/atlas/gameass/DDZ.atlas");
            anim.interval = 200;
            anim.play(0,true);
            this.owner.addChild(anim);
            anim.x = 300;
            anim.y = 300;
        }));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值