cocos 九宫格 抽奖

        this.button.eOnClick(() => {
            this._displayInterval = 0.1;
            const items = this.drawArea.children;
            let currentIndex = 0;
            let rounds = 0;

            const showIcon = () => {
                if (rounds >= 2 && currentIndex - 1 === this.prizeIndex) {
                    this.info('333');
                    return; // 终止递归
                }
                if (currentIndex >= items.length) {
                    currentIndex = 0;
                    rounds++;
                }
                const currentItem = items[currentIndex];
                const iconNode = currentItem.getChildByName('icon');
                items.forEach((item) => {
                    const icon = item.getChildByName('icon');
                    if (icon) {
                        icon.active = false; // 隐藏所有 "icon"
                    }
                });
                this._displayInterval += 0.01;
                if (iconNode) {
                    iconNode.active = true; // 显示 "icon"
                    this.scheduleOnce(() => {
                        iconNode.active = false; // 隐藏 "icon"
                        currentIndex++;
                        showIcon();
                    }, this._displayInterval);
                } else {
                    currentIndex++;
                    showIcon();
                }
            };
            showIcon();
})

    prizeIndex: number = 0; // 中奖的奖项索引,可以根据需求调整

    private _displayInterval: number = 0.1; // 切换 "icon" 显示状态的时间间隔

    // 抽奖区

    @property(cc.Node)

    protected drawArea: cc.Node = null;

    // 抽奖按钮

    @property(cc.Node)

    protected button: cc.Node = null;

 在引擎上,创建一个九宫格,每个格子里面,添加一个icon,控制这个icon,循环的显示与关闭,就能够达到九宫格抽奖的效果。

如果想要速度由块到慢,就逐步添加_displayInterval 的值,如果想保持就不添加,如果想由慢到块,就逐步减少它的值。 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值