uniApp 中实现一个骰子动效

代码如下

复制代码
<template>
	<!-- 骰子组件 -->
	<view class="dice-wrap" @tap="throwDice">
		<!-- 筛子运动时候的展示的图片 -->
		<image v-if="isDicing" :src="diceAnimationImages[aniIndex]" class="dice-icon"></image>
		<!-- 筛子静止时候的显示的对应点数的图片 -->
		<image v-else :src="diceImages[currentPoint]" class="dice-icon"></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//是否正在掷筛子
				isDicing: false,
				//当前显示的动画图片索引
				aniIndex: 0,
				//掷筛子动画所用到的图片(4张骰子旋转过程中的图)
				diceAnimationImages: [
					'https://www.upupo.cn/img/骰子素材/动1.png',
					'https://www.upupo.cn/img/骰子素材/动2.png',
					'https://www.upupo.cn/img/骰子素材/动3.png',
					'https://www.upupo.cn/img/骰子素材/动4.png'
				],
				//骰子每一个点的对应的图片
				diceImages: {
					//1点的图片
					1: 'https://www.upupo.cn/img/骰子素材/1点.png',
					//2点的图片
					2: 'https://www.upupo.cn/img/骰子素材/2点.png',
					//3点的图片
					3: 'https://www.upupo.cn/img/骰子素材/3点.png',
					//4点的图片
					4: 'https://www.upupo.cn/img/骰子素材/4点.png',
					//5点的图片
					5: 'https://www.upupo.cn/img/骰子素材/5点.png',
					//6点的图片
					6: 'https://www.upupo.cn/img/骰子素材/6点.png'
				},
				//当前掷筛子掷到的点数(默认1点)
				currentPoint: 1,
				//定时器
				timer: null,
			}
		},
		beforeDestroy() {
			//组件销毁之前清除定时器
			clearInterval(this.timer);
		},
		methods: {
			async getDicePoint() {
				let point = 1;
				//从1~6随机一个数
				point = Math.floor(Math.random() * 6 + 1);
				return point;
			},
			//掷骰子
			async throwDice() {
				//如果当前骰子正在滚动则不能掷骰子
				if (this.isDicing) {
					return
				}
				//从接口获取点数
				this.currentPoint = await this.getDicePoint();
				//开启骰子动画
				await this.startAnimation();
				//动画完毕之后可以通知父组件当前掷到的点数
				this.$emit('throwEnd', this.currentPoint);
			},
			//开启动画效果
			async startAnimation() {
				return new Promise((resolve) => {
					//设置筛子开始运动
					this.isDicing = true;
					//记录动画次数
					let num = 0;
					//每隔100毫秒来回切换一张"动"图形成掷骰子的动画
					this.timer = setInterval(() => {
						let index = this.aniIndex;
						index++;
						if (index >= this.diceAnimationImages.length) {
							index = 0;
						}
						this.aniIndex = index;
						num++;
						//差不多执行1.2秒钟的时候可以停止了
						if (num > 12) {
							//关闭定时器
							clearInterval(this.timer);
							//设置骰子停止
							this.isDicing = false;
							//返回结果
							resolve(true);
						}
					}, 100);
				})
			},
		}
	}
</script>

<style lang="scss">
	.dice-wrap {
		width: 172rpx;
		height: 172rpx;

		.dice-icon {
			width: 172rpx;
			height: 172rpx;
		}
	}
</style>

素材地址:https://download.csdn.net/download/yueye_wu/88621174

ps:素材免费下载哦

相关推荐
2501_9160074715 小时前
苹果手机iOS应用管理全指南与隐藏功能详解
android·ios·智能手机·小程序·uni-app·iphone·webview
2501_9151063217 小时前
全面理解 iOS 帧率,构建从渲染到系统行为的多工具协同流畅度分析体系
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088919 小时前
iOS 能耗检测的工程化方法,构建多工具协同的电量分析与性能能效体系
android·ios·小程序·https·uni-app·iphone·webview
济南壹软网络科技有限公司20 小时前
综合社交服务平台的技术架构与实践:构建高可用、多端覆盖的互动生态
uni-app·php·开源源码·陪玩陪聊h5
2501_9159214320 小时前
重新理解 iOS 的 Bundle Id 从创建、管理到协作的工程策略
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063220 小时前
当 altool 退出历史舞台,iOS 上传链路的演变与替代方案的工程实践
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张21 小时前
Transporter 的局限与替代路径,iOS 上传流程在多平台团队中的演进
android·ios·小程序·https·uni-app·iphone·webview
lwprain21 小时前
uniapp使用uniview-plus性能问题处理
uni-app
00后程序员张21 小时前
Python 抓包工具全面解析,从网络监听、协议解析到底层数据流捕获的多层调试方案
开发语言·网络·python·ios·小程序·uni-app·iphone
AH_HH1 天前
UniApp H5 代理失效的终极替代方案
uni-app