[CocosCreator]TS装饰器妙用

(老规矩:广告位留给自己)

        欢迎喜欢或者从事CocosCreator开发的小伙伴请加入我的大家庭CocosCreator游戏开发Q群:26855530 

        首先,让我了解什么是装饰器(cp:网上一段材料)

        装饰器(Decorator) 仅提供定义劫持,能够对类及其方法、方法入参、属性的定义并没有提供任何附加元数据的功能。主要是为了提供附加的功能

        其实按我个人理解,他就类似Java里常说的代理模式:在不影响代理目标对象的前提下,完成对代理对象的增强,简单理解就是AOP,再简单点就是补刀!

        好了废话多说.上正菜:

1.要使用装饰器需要配置使用条件,即在tsconfig.json里确保最低支持ES5版本以及启用experimentalDecorators编译器,如下:

{
    "compilerOptions": {
        "target": "ES5",
        "experimentalDecorators": true
    }
}

2.装饰器含有五种类型,分别是添加在类、属性、方法、参数和访问者头上的@修饰符;主要功能就是在运行时,观察或是修改它们的装饰对象。

  • Class Decorator (类修饰器)
  • Property Decorator (属性修饰器)
  • Method Decorator (方法修饰器)
  • Parameter Decorator(参数修饰器)
  • Accessor Decorator (存取修饰器)

今天主要讲的重点是方法修饰器,也是使用最多的,其他的也类似,有兴趣的小伙伴自行学习就好了

/**
 * 按钮点击限制节流
 * @param lockTime 阻塞时间
 * @param callBackFun 节流回调
 */
export function ButtonLock(lockTime: number = 0.3, callBackFun?: Function) {
    return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
        let oldFun: Function = descriptor.value;
        let isLock: boolean = false;
        descriptor.value = function (...args: any[]) {
            if (isLock) {
                if (callBackFun) {
                    callBackFun();
                }
                return
            }
            isLock = true;
            setTimeout(() => {
                isLock = false
            }, lockTime * 1000);
            oldFun.apply(this, args);
        }
        return descriptor
    }
}

这是我写的按钮限流装饰器,他的作用是防止用户点击过快所使用的,代码本生很简单,就是在进入时把代码锁住,在指定时间内无法继续访问目标函数,以达到限流的效果.

其中:

  • target:当前对象的原型链,以 Persion 类为例,它就是 Person.prototype
  • propertyKey:方法的名字:如 getFullName
  • descriptor:方法的描述,也就是Object.getOwnPropertyDescriptor(Person.prototype, propertyKey)

定义完我们的装饰器,只要在我们的按钮函数上使用既可以

@ButtonLock(1, () => {
    cc.log("操作太快了,请稍后再试");
})
buttonEvent(event: cc.Event.EventCustom, customEventData: string) {
    event.stopPropagation();
    cc.log("按钮逻辑");
}

效果就是:该按钮只能在1秒内被触发一次,并提供友好提示,简单好用!

小伙伴们,下期见!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Cocos Creator中使用TypeScript来创建弹窗控制器,你可以按照以下步骤进行操作: 1. 创建一个名为PopupManager的类,该类将负责管理所有弹窗的打开和关闭操作。 ```typescript export class PopupManager { private static _instance: PopupManager = null; private _popupStack: cc.Node[] = []; private _popupLayer: cc.Node = null; public static getInstance(): PopupManager { if (PopupManager._instance == null) { PopupManager._instance = new PopupManager(); } return PopupManager._instance; } private constructor() { this._popupLayer = new cc.Node(); this._popupLayer.name = "PopupLayer"; cc.director.getScene().addChild(this._popupLayer); } public pushPopup(popup: cc.Node) { if (popup == null) { return; } this._popupLayer.addChild(popup); this._popupStack.push(popup); } public popPopup() { if (this._popupStack.length > 0) { let popup = this._popupStack.pop(); popup.removeFromParent(true); } } } ``` 2. 创建一个名为PopupBase的类,该类将作为所有弹窗的基类,提供弹窗的打开和关闭方法。你可以在该类的onLoad方法中进行初始化操作。 ```typescript export class PopupBase extends cc.Component { public onLoad() { // 初始化弹窗界面 this.initView(); } public initView() { // 子类需要实现该方法,用于初始化弹窗界面 } public show() { // 弹出弹窗 PopupManager.getInstance().pushPopup(this.node); } public hide() { // 隐藏弹窗 PopupManager.getInstance().popPopup(); } } ``` 3. 创建一个名为MyPopup的类,该类将作为你的具体弹窗控制器,在该类中继承PopupBase类。 ```typescript export class MyPopup extends PopupBase { private _closeBtn: cc.Node = null; public initView() { // 初始化弹窗界面 this._closeBtn = this.node.getChildByName("CloseBtn"); this._closeBtn.on(cc.Node.EventType.TOUCH_END, this.onCloseBtnClick, this); } private onCloseBtnClick() { // 关闭弹窗 this.hide(); } } ``` 4. 在场景中添加一个空节点作为弹窗容器,并将MyPopup预制体添加到该节点下。 ```typescript const {ccclass, property} = cc._decorator; @ccclass export class MainScene extends cc.Component { @property(cc.Prefab) private _myPopupPrefab: cc.Prefab = null; private _myPopup: cc.Node = null; public onLoad() { // 加载弹窗预制体 this._myPopup = cc.instantiate(this._myPopupPrefab); } public onBtnShowPopupClick() { // 显示弹窗 this._myPopup.getComponent(MyPopup).show(); } } ``` 现在你已经创建了一个弹窗控制器,可以通过该控制器来方便地管理多个弹窗,并在需要的时候打开或关闭它们。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值