当前位置:   article > 正文

egret eui 动态添加多个按钮_菜鸟丨如何用Egret实现抽奖轮盘

egret 如何画一个按钮

本篇教程适合初次接触白鹭的同学,我们通过实现抽奖效果,让大家对egret的touch事件,timer计时器以及滤镜有一个基本的了解。具体效果如下:

69310d44f2cabf977bd7b82a84a9aa43.gif

教程源码地址:https://github.com/hkjlx/Luckdraw

具体实现方法

1、创建一个Luck类编写抽奖的主逻辑

Luck类继承egret.DisplayObjectContainer容器

  1. class Luck extends egret.DisplayObjectContainer {
  2. public constructor() {
  3. super();
  4. }
  5. }

2、数据初始化

  1. //存放所有奖品的数组
  2. private arr_img: string[] = [];
  3. //抽奖计时器
  4. private tim:egret.Timer;
  5. //初始化
  6. private init() {
  7. //创建中心区域的红色抽奖按钮
  8. this.creatbtn();
  9. //给存放奖品的数组赋值
  10. this.arr_img = ["xg_png", "ct_png", "lj_png", "zs_png", "my_png", "mg_png", "hua_png", "yy_png", "jct_png", "xr_png"];
  11. //绘制轮盘使奖品以圆的形式排列
  12. this.setcircle();
  13. //添加抽奖时的计时器
  14. this.tim = new egret.Timer(500,0);
  15. this.tim.addEventListener(egret.TimerEvent.TIMER,this.Luckyanimation,this);
  16. }

3、创建红色抽奖按钮(creatbtn)

设置抽奖按钮居中:改变按钮的锚点,默认的锚点在左上角,改为按钮的中心this.btn.anchorOffsetX = this.btn.width / 2;this.btn.anchorOffsetY = this.btn.height / 2;然后让按钮的位置在整个舞台的中心。此时在创建按钮的时候舞台上什么都没有,所以直接用this.stage.stageWidth是获取不到舞台的宽高的。在以后的项目中同学们可以创建一个date类,用来存放全局的数据,本篇教程是在main.ts中直接添加了两个静态属性STAGEWIDTHSTAGEHEIGHT具体方法如下:

  1. //舞台宽度
  2. public static STAGEWIDTH: number;
  3. //舞台高度
  4. public static STAGEHEIGHT: number;
  5. protected createGameScene(): void {
  6. Main.STAGEWIDTH = this.stage.stageWidth;
  7. Main.STAGEHEIGHT = this.stage.stageHeight;
  8. this.addChild(new Luck);
  9. }
  10. private btn: egret.Sprite;//中心的抽奖按钮
  11. //绘制抽奖按钮
  12. private creatbtn() {
  13. //创建抽奖按钮的精灵
  14. this.btn = new egret.Sprite();
  15. this.addChild(this.btn);
  16. //让按钮居中
  17. this.btn.x = Main.STAGEWIDTH / 2;
  18. this.btn.y = Main.STAGEHEIGHT / 2;
  19. this.btn.anchorOffsetX = this.btn.width / 2;
  20. this.btn.anchorOffsetY = this.btn.height / 2;
  21. //开启btn的可点击
  22. this.btn.touchEnabled = true;
  23. //给btn添加点击事件
  24. this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onbtnTap, this);
  25. //绘制红色的圆并添加到btn中
  26. let cir:egret.Shape = new egret.Shape();
  27. cir.graphics.beginFill(0xff0000);
  28. cir.graphics.drawCircle(0,0,50);
  29. cir.graphics.endFill();
  30. this.btn.addChild(cir);
  31. //添加抽奖文字
  32. let cj:egret.TextField = new egret.TextField();
  33. cj.text = '抽奖';
  34. cj.anchorOffsetX = cj.width/2;
  35. cj.anchorOffsetY = cj.height/2;
  36. cj.textColor = 0xd8ee1f;
  37. this.btn.addChild(cj);
  38. }

4、使所有奖品以圆的形式排列

  1. private point = []; //结果
  2. private bitmapArr:egret.Bitmap[] = [];//
  3. //让奖品以圆的形式排列
  4. private setcircle() {
  5. this.getPoint(200, Main.STAGEWIDTH / 2, Main.STAGEHEIGHT / 2, this.arr_img.length);
  6. for (let i: number = 0; i < this.point.length; i++) {
  7. let img = new egret.Bitmap();
  8. img.texture = RES.getRes(this.arr_img[i]);
  9. img.width = 60;
  10. img.height = 60;
  11. img.anchorOffsetX = img.width / 2;
  12. img.anchorOffsetY = img.height / 2;
  13. //根据拿到的坐标给每一个奖品赋值
  14. img.x = this.point[i].x;
  15. img.y = this.point[i].y;
  16. img.name = i+'';
  17. this.addChild(img);
  18. this.bitmapArr.push(img);
  19. }
  20. }
  21. /**
  22. * 求圆周上等分点的坐标
  23. * @param r 圆的半径
  24. * @param ox 圆心坐标x
  25. * @param oy 圆心坐标y
  26. * @param count 等分个数
  27. */
  28. private getPoint(r: number, ox: number, oy: number, count: number) {
  29. let radians = (Math.PI / 180) * Math.round(360 / count); //弧度
  30. for (let i: number = 0; i < count; i++) {
  31. let x = ox + r * Math.sin(radians * i);
  32. let y = oy + r * Math.cos(radians * i);
  33. this.point.unshift({ x: x, y: y }); //为保持数据顺时针
  34. }
  35. }

在egret中,用/***/进行注释会在编写代码时有代码提示,当参数比较多时,可在注释行中用@param声明每个参数的作用。

6eb3069410029c6fdb43b77163b5be84.png

5、编写点击抽奖的响应事件

在点击事件里移除对当前点击的监听,防止用户多次点击。

在计时器里可以直接用 timer.delay 改变计时器的延迟毫秒数。

  1. private rad:number;
  2. private isjia:boolean = true;
  3. //点击开始抽奖
  4. private onbtnTap() {
  5. //移除点击事件监听
  6. this.btn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onbtnTap, this);
  7. //获取一个随机数
  8. this.Getrandom();
  9. //开启计时器
  10. this.tim.start();
  11. //
  12. this.isjia = true;
  13. this.jsq = 0;
  14. }
  15. private jsq:number=0; //控制计时器在延迟小于30的时候实现一个抽奖滚动的过程
  16. //抽奖动画特效
  17. private Luckyanimation(){
  18. this.num++;
  19. if (this.num>=this.arr_img.length) this.num =0;
  20. this.creatfilter(this.bitmapArr[this.num==0?this.arr_img.length-1:this.num-1],this.bitmapArr[this.num]);
  21. //实现由慢到快,再由快到慢的过程
  22. if(this.tim.delay>30 && this.isjia){
  23. this.tim.delay-=30;
  24. }else{
  25. this.jsq++;
  26. }
  27. if(this.jsq>=100){
  28. this.isjia = false;
  29. this.tim.delay+=20;
  30. }
  31. if(!this.isjia && this.num==this.rad && this.tim.delay>400){
  32. this.tim.stop();
  33. //当计时器停止并且已经停在了抽奖的位置时,再给抽奖按钮添加监听
  34. this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onbtnTap, this);
  35. }
  36. }
  37. //获取随机出现的值
  38. private Getrandom(){
  39. this.rad = Math.floor(Math.random()*this.arr_img.length);
  40. }
  41. //设置滤镜
  42. private creatfilter(bitmap0:egret.Bitmap,bitmap:egret.Bitmap) {
  43. var color: number = 0x33CCFF; /// 光晕的颜色,十六进制,不包含透明度
  44. var alpha: number = 0.8; /// 光晕的颜色透明度,是对 color 参数的透明度设 定。有效值为 0.01.0。例如,0.8 设置透明度值为 80%。
  45. var blurX: number = 35; /// 水平模糊量。有效值为 0255.0(浮点)
  46. var blurY: number = 35; /// 垂直模糊量。有效值为 0255.0(浮点)
  47. var strength: number = 2; /// 压印的强度,值越大,压印的颜色越深,而且发光与背景之间的对比度也越强。有效值为 0255。暂未实现
  48. var quality: number = egret.BitmapFilterQuality.HIGH; /// 应用滤镜的次数,建议用 BitmapFilterQuality 类的常量来体现
  49. var inner: boolean = false; /// 指定发光是否为内侧发光
  50. var knockout: boolean = false; /// 指定对象是否具有挖空效果
  51. var glowFilter: egret.GlowFilter = new egret.GlowFilter(color, alpha, blurX, blurY, strength, quality, inner, knockout);
  52. bitmap0.filters=[];//先清除上一个的发光滤镜,再给当前图片添加
  53. bitmap.filters = [glowFilter];
  54. }

以上就是本次教程的全部内容了,不知道各位开发者小伙伴们看完之后有没有学会,如果有任何关于白鹭引擎的问题,欢迎在文章内评论,我们共同交流探讨。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54443?site
推荐阅读
相关标签
  

闽ICP备14008679号