温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LayaAir之引入模块(module)编程方案

发布时间:2020-06-24 02:41:14 来源:网络 阅读:3105 作者:Aonaufly 栏目:开发技术

LayaAir在引入类等方面确实没有Egret做的好(比较麻烦),本人喜欢模块,所以给出了在Laya中使用模块(module)的解决方案.


一 : 关于MaskDemo.ts的写法

export module demo{
    export  class MaskDemo{
        private Res : string = null;
        private img : Laya.Sprite = null;
        private cMask : Laya.Sprite = null;
        public constructor(){
            Laya.init(1336,640);
            Laya.stage.bgColor = "#ffffff";
            this.Res = "res/atlas/comp.png";
            Laya.loader.load( this.Res , Laya.Handler.create(this,this.graphicsImg) );
        }
        private graphicsImg() : void{
            this.img = new Laya.Sprite();
            this.img.graphics.drawTexture(Laya.loader.getRes(this.Res), 300 , 100);
            Laya.stage.addChild(this.img);

            // this.cMask = new Laya.Sprite();
            // this.cMask.graphics.drawCircle(80,80,50,"#ff0000");
            // this.cMask.pos(120,50);
            // this.img.mask = this.cMask;
        }
    }
}

注意 :
①,module前面也要以export修饰

二:在Main.ts中引用
Ps : MaskDemo.ts和Main.ts在一个目录里面
LayaAir之引入模块(module)编程方案

import { demo } from "./MaskDemo";

注意: ①{}里面写入module名称

使用 : let $mask : demo.MaskDemo = new demo.MaskDemo();

-扩展(如果MaskDemo在script里面)
LayaAir之引入模块(module)编程方案
import { demo } from "./script/MaskDemo";

补充 , 另一种方案
import demo = require("./demo/MaskDemo");
import demo1 = require("./demo/DrawShapes");

运用
let $mask : demo.demo.MaskDemo = new demo.demo.MaskDemo();
let $draw : demo1.demo.DrawShapes = new demo1.demo.DrawShapes();

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI