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

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();

转载于:https://blog.51cto.com/aonaufly/2287059

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值