cocosCreator实现两种效果转盘抽奖

2024-04-12 17:36

本文主要是介绍cocosCreator实现两种效果转盘抽奖,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言: cocosCreator实现两种效果转盘抽奖。

今天完成策划给的2个抽奖需求,我把核心逻辑提取出来并拓展了一个功能更丰富的效果,抛砖引玉分享出来。
远程Gitee: 稍后上传

一、转盘抽奖

A:使用cc.tween().to功能+缓动函数实现旋转。
优点:简单易懂,方便控制。
缺点:功能单一
在这里插入图片描述
B:使用适应对时间的插值计算 + cc.tween().by 。
奖励展示跟随指针选中效果
使用了es6写法,对于页游可能在部分浏览器上不支持(比如360的兼容模式),可改成es写法
在这里插入图片描述

二、轮询抽奖

同样使用了2中方式实现。
在这里插入图片描述

三、代码展示(展示转盘抽奖B方案)

节点树和脚本挂载
在这里插入图片描述

GameControl脚本

import GoLuckDraw from "./GoLuckDraw";const { ccclass, property } = cc._decorator;@ccclass
export default class GameControl extends cc.Component {@property(cc.Node)public RewardPre: cc.Node = null;   //奖励预制节点  public AwardNode: cc.Node;         //奖励父节public Pointer: cc.Node;           //指针public LuckDraw: GoLuckDraw;       //旋转组件public rewardCount: number = 12;                     //奖品个数      public avgAngle: number = 360 / this.rewardCount;   //平均角度private angleStep = Math.PI * 2 / this.rewardCount; //平均弧度private radius: number = 240;                       //半径private curIndex: number = 0;    //当前奖励下标private tagetIndex: number = 0;  //目标奖励下标private reawadArr: Array<Number> = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33];  //奖品名称onLoad() {this.AwardNode = cc.find("ZhuanPanRoot/BackGround/AwardNode", this.node);this.Pointer = cc.find("ZhuanPanRoot/BackGround/CentreNode/Pointer", this.node);this.LuckDraw = cc.find("ZhuanPanRoot", this.node).getComponent(GoLuckDraw);}start() {this._InitAwardPre();}private _InitAwardPre() {for (let i = 0; i < this.rewardCount; i++) {const angle = i * this.angleStep; // 计算当前点的角度  const x = this.radius * Math.cos(angle); // 使用余弦计算x偏移量        const y = this.radius * Math.sin(angle); // 使用正弦计算y偏移量  let rewardPre = cc.instantiate(this.RewardPre);rewardPre.x = y, rewardPre.y = x;rewardPre.parent = this.AwardNode;rewardPre.active = true;rewardPre.getChildByName('label').getComponent(cc.Label).string = i.toString();}}//开始抽奖private isTurnIng = false;public BtnCallBack() {if (this.isTurnIng) {return;}this.isTurnIng = true;let awardIndex = Math.floor(Math.random() * this.rewardCount);this.tagetIndex = awardIndex;console.log("========>>>>", awardIndex, this.reawadArr[awardIndex]);this.LuckDraw.StartLuckDraw(this, this.curIndex, this.tagetIndex, 4, 4);this.LuckDraw.CompleteCallBack = () => {this.curIndex = this.tagetIndex;this.isTurnIng = false;}}// update (dt) {}
}

GoLuckDraw

import GameControl from "./GameControl";const { ccclass, property } = cc._decorator;@ccclass
export default class GoLuckDraw extends cc.Component {public CompleteCallBack: () => void;private EndDarwTick: number = 2;    //最后一轮的秒数为2秒;private CurIndex: number;           //起始位置private StopIndex: number;          //停止位置private Allticks: number;           //总秒数private MaxCount: number;           //轮数private MainThis: GameControl;private ByAngle: number;onLoad() {}/*** * @param curIndex   起始位置* @param stopIndex  结束位置* @param allticks   总时长* @param maxCount      轮次*/public StartLuckDraw(gameControl: GameControl, curIndex: number, stopIndex: number, allticks: number, maxCount: number) {this.MainThis = gameControl;this.ByAngle = this.MainThis.avgAngle * -1;this.CurIndex = curIndex;this.StopIndex = stopIndex + this.MainThis.rewardCount;this.Allticks = allticks > 3 ? allticks : 3;this.MaxCount = maxCount;this.LuckDrawRounds();}private async LuckDrawRounds() {let time1 = 0, time2 = 0;let sumCount = this.MaxCount * this.MainThis.rewardCount;let avgTime = (this.Allticks - this.EndDarwTick) / sumCount;  //平均时间for (let i = this.CurIndex; i < sumCount; i++) {time1 = cc.misc.lerp(0, avgTime * 2, (1 / sumCount) * (i + 1));cc.tween(this.MainThis.Pointer).by(time1, { angle: this.ByAngle }).start();this.SetAwardState(i);await this.SleepTime(time1);}avgTime = this.EndDarwTick / this.StopIndex;for (let i = 0; i < this.StopIndex; i++) {time2 = cc.misc.lerp(time1, avgTime * 2, (1 / this.StopIndex) * (i + 1));cc.tween(this.MainThis.Pointer).by(time2, { angle: this.ByAngle  }).start();this.SetAwardState(i);await this.SleepTime(time2);}this.SetAwardState(this.StopIndex);await this.SleepTime(1);if (this.CompleteCallBack != null) {this.CompleteCallBack();}}private SleepTime(time: number): Promise<void> {return new Promise((resolve, reject) => {this.scheduleOnce(() => {resolve();}, time);})}private SetAwardState(index: number) {let _index = index % this.MainThis.rewardCount;let label: cc.Node;let lastIndex = _index - 1 < 0 ? this.MainThis.rewardCount - 1 : _index - 1;label = this.MainThis.AwardNode.children[lastIndex]?.getChildByName("label");if (label) {label.color = cc.Color.WHITE;}label = this.MainThis.AwardNode.children[_index]?.getChildByName("label");if (label) {label.color = cc.Color.RED;}}
}

这篇关于cocosCreator实现两种效果转盘抽奖的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/897793

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依