【cocos creator】弹窗提示,按钮点击传一个回调函数,在点击按钮后执行

按钮点击传一个回调函数,在点击按钮后执行

//挂在按钮所在panel上
// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        msg: cc.Label,
        btn_A: cc.Button,
        btn_B: cc.Node,
        text1: cc.Label,
        text2: cc.Label,
        lizi: cc.Prefab,
        liziParent: cc.Node,
        TipNode: cc.Node,
        setfunc: false,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.node.x = cc.winSize.width / 2;
        this.node.y = cc.winSize.height / 2;


        //this.touch();
        this.showOne();
    },

    start() {
    },

    setMsg(str) {
        this.msg.string = str;
    },

    // LIFE-CYCLE CALLBACKS:


    touch() {
        //点击粒子特效
        var self = this;
        this.liziParent.on(cc.Node.EventType.TOUCH_START, function (event) {
            var touches = event.getTouches();
            if (touches.length !== 1) {
                return;
            }
            var touchLoc = touches[0].getLocation();
            self.lizinode = cc.instantiate(self.lizi);//克隆预制体
            self.lizinode.parent = self.node;//设置父节点
            self.lizinode.x = touchLoc.x - cc.winSize.width / 2;
            self.lizinode.y = touchLoc.y - cc.winSize.height / 2;
        });
        this.liziParent.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
            var touches = event.getTouches();
            var touchLoc = touches[0].getLocation();
            self.lizinode.x = touchLoc.x - cc.winSize.width / 2;
            self.lizinode.y = touchLoc.y - cc.winSize.height / 2;
        });
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, function () {
            if (self.lizinode) {
                self.lizinode.destroy();
            }
        });
        this.node.on(cc.Node.EventType.TOUCH_END, function () {
            if (self.lizinode) {
                self.lizinode.destroy();
            }
        });
    },

    hidBtn() {
        this.btn_A.node.active = false;
    },

    btn() {
        if (this.setfunc) {
            this.Function();
        }
        this.node.destroy();
    },

    showOne() {
        this.btn_B.active = false;
        this.btn_A.node.active = true;
    },
    
    showTwo() {
        this.btn_B.active = true;
        this.btn_A.node.active = false;
    },

    close() {
        this.node.destroy();
    },

    setText(str) {
        this.text1.string = str;
        this.text2.string = str;
    },

    setReGame() {
        //this.setFunction(function () { require('GameUtils').loadScene("LoginScene"); });
    },

    setFunction(fun) {
        this.Function = fun;
        this.setfunc = true;
    },
    // update (dt) {},
});

使用:


    showTip(msg, hidBtn, str, callBackfunction) {
        require("AudioManager").Instance().playerBtnCheck(1);
        this.tip = cc.instantiate(this.tips);
        this.tip.getComponent("Tips").setMsg(msg);
        this.tip.parent = this.node;
        if (hidBtn === 0) {
            this.tip.getComponent("Tips").hidBtn();
        }
        else if (hidBtn === 1) {
        }
        else if (hidBtn === 2) {
            this.tip.getComponent("Tips").showTwo();
        }
        else if (hidBtn === -1) {
            this.tip.getComponent("Tips").setReGame();
        }
        if (str) {
            this.tip.getComponent("Tips").setText(str);
        }
        if (callBackfunction) {
            this.tip.getComponent("Tips").setFunction(callBackfunction);
        }
        else if (!hidBtn) {
        }
    },
    destoryTip() {
        if (this.tip) {
            this.tip.destroy();
        }
    },

简易版:(TS)

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export class SomeInfo extends cc.Component {
    private showCloseBtn = true;
    private yesCb: () => void;
    showInfo(info: string, showCloseBtn: boolean = false, yesCb?: () => void) {
        this.node.getChildByName("label").getComponent(cc.Label).string = info;
        this.yesCb = yesCb;
        this.showCloseBtn = showCloseBtn;
        if (!showCloseBtn) {
            this.node.getChildByName("btn_close").active = false;
        }
    }

    btn_close() {
        if (this.showCloseBtn) {
            this.node.destroy();
        }
    }

    btn_yes() {
        this.node.destroy();
        if (this.yesCb) {
            this.yesCb();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,你需要在游戏启动时获取当前时间,并计算距离第二天0点的时间差,然后将这个时间差存储起来。 然后在按钮点击时,判断当前时间是否已经过了第二天0点,如果已经过了,则将按钮状态设置为可点击,并更新存储的时间差;如果还没过,则不做任何操作。 下面是一个简单的实现示例: ```javascript // 获取当前时间 var now = new Date(); // 计算距离第二天0点的时间差 var diff = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1) - now; // 存储时间差 cc.sys.localStorage.setItem('buttonCooldown', diff); // 按钮点击事件 button.on('click', function() { // 获取存储的时间差 var cooldown = parseInt(cc.sys.localStorage.getItem('buttonCooldown')); if (cooldown <= 0) { // 时间差已经过了第二天0点,可以点击按钮 button.interactable = true; // 更新时间差为下一个24小时距离现在的时间差 cc.sys.localStorage.setItem('buttonCooldown', 24 * 60 * 60 * 1000); } else { // 时间差还没过,按钮不可点击 button.interactable = false; } }); // 定时器,每秒更新一次时间差 setInterval(function() { // 获取存储的时间差 var cooldown = parseInt(cc.sys.localStorage.getItem('buttonCooldown')); if (cooldown > 0) { // 时间差还没过,更新时间差 cooldown -= 1000; cc.sys.localStorage.setItem('buttonCooldown', cooldown); } }, 1000); ``` 需要注意的是,这个示例中只是简单地使用本地存储来存储时间差,如果用户清除了浏览器缓存或者更换了设备,时间差就会丢失。如果需要更可靠的存储方式,可以考虑使用服务器存储或者使用浏览器的 IndexedDB 或 WebSQL 等本地存储技术。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值