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

相关文章

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

通过SSH隧道实现通过远程服务器上外网

搭建隧道 autossh -M 0 -f -D 1080 -C -N user1@remotehost##验证隧道是否生效,查看1080端口是否启动netstat -tuln | grep 1080## 测试ssh 隧道是否生效curl -x socks5h://127.0.0.1:1080 -I http://www.github.com 将autossh 设置为服务,隧道开机启动

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测 目录 时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测基本介绍程序设计参考资料 基本介绍 MATLAB实现LSTM时间序列未来多步预测-递归预测。LSTM是一种含有LSTM区块(blocks)或其他的一种类神经网络,文献或其他资料中LSTM区块可能被描述成智能网络单元,因为

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主

android一键分享功能部分实现

为什么叫做部分实现呢,其实是我只实现一部分的分享。如新浪微博,那还有没去实现的是微信分享。还有一部分奇怪的问题:我QQ分享跟QQ空间的分享功能,我都没配置key那些都是原本集成就有的key也可以实现分享,谁清楚的麻烦详解下。 实现分享功能我们可以去www.mob.com这个网站集成。免费的,而且还有短信验证功能。等这分享研究完后就研究下短信验证功能。 开始实现步骤(新浪分享,以下是本人自己实现

基于Springboot + vue 的抗疫物质管理系统的设计与实现

目录 📚 前言 📑摘要 📑系统流程 📚 系统架构设计 📚 数据库设计 📚 系统功能的具体实现    💬 系统登录注册 系统登录 登录界面   用户添加  💬 抗疫列表展示模块     区域信息管理 添加物资详情 抗疫物资列表展示 抗疫物资申请 抗疫物资审核 ✒️ 源码实现 💖 源码获取 😁 联系方式 📚 前言 📑博客主页:

探索蓝牙协议的奥秘:用ESP32实现高质量蓝牙音频传输

蓝牙(Bluetooth)是一种短距离无线通信技术,广泛应用于各种电子设备之间的数据传输。自1994年由爱立信公司首次提出以来,蓝牙技术已经经历了多个版本的更新和改进。本文将详细介绍蓝牙协议,并通过一个具体的项目——使用ESP32实现蓝牙音频传输,来展示蓝牙协议的实际应用及其优点。 蓝牙协议概述 蓝牙协议栈 蓝牙协议栈是蓝牙技术的核心,定义了蓝牙设备之间如何进行通信。蓝牙协议

以canvas方式绘制粒子背景效果,感觉还可以

这个是看到项目中别人写好的,感觉这种写法效果还可以,就存留记录下 就是这种的背景效果。如果想改背景颜色可以通过canvas.js文件中的fillStyle值改。 附上demo下载地址。 https://download.csdn.net/download/u012138137/11249872

echarts省份标注加散点效果

这个是安徽的效果图,鼠标移到红色标注或者对应的市区位置都会显示对应的数值。 先直接上代码: import anhuiMapJson from './anhui.json'getCoords: function(city) {var res = [];if (city != null) {for (var c in this.cityMap.features) {if (this.cityMa

python实现最简单循环神经网络(RNNs)

Recurrent Neural Networks(RNNs) 的模型: 上图中红色部分是输入向量。文本、单词、数据都是输入,在网络里都以向量的形式进行表示。 绿色部分是隐藏向量。是加工处理过程。 蓝色部分是输出向量。 python代码表示如下: rnn = RNN()y = rnn.step(x) # x为输入向量,y为输出向量 RNNs神经网络由神经元组成, python