注册登录

微信小程序翻牌游戏如何做,微信小程序翻牌游戏实现效果

2018-09-20
导读:背景 ps:本次开发基于wepy框架 由于最近接到一个需求--抽奖活动; 翻牌打乱活动抽奖活动,大概需求是这样的,九宫格卡牌,先正面展示所有奖品,然后卡牌翻转,打乱排序,点击卡...

背景

ps:本次开发基于wepy框架
由于最近接到一个需求--抽奖活动;
翻牌打乱活动抽奖活动,大概需求是这样的,九宫格卡牌,先正面展示所有奖品,然后卡牌翻转,打乱排序,点击卡牌,然后抽奖。

这个需求本身其实不难,主要是分为三步;


  1. 展示所有卡牌,然后翻转。
  2. 打乱所有卡牌
  3. 点击其中一张卡牌,抽奖

第一步:卡牌翻转

我们先在dom中渲染9个卡牌。


  1. {{cardItem.front}}
  2. {{cardItem.back}}

script


  1. allMove () {
  2. // 110 是卡牌宽度加边距
  3. this.methods.shuffle.call(this, 110)
  4. let timer = setTimeout(() => {
  5. clearTimeout(timer)
  6. this.methods.shuffle.call(this, 0)
  7. this.$apply()
  8. }, 1000)
  9. },
  10. // 洗牌
  11. shuffle (translateUnit) {
  12. let curCardData = this.cardData
  13. curCardData.map((item, index) => {
  14. let animation = wepy.createAnimation({
  15. duration: 500,
  16. timingFunction: 'ease'
  17. })
  18. animation.export()
  19. switch (index) {
  20. case 0:
  21. animation.translate(translateUnit, translateUnit).step()
  22. break
  23. case 1:
  24. animation.translate(0, translateUnit).step()
  25. break
  26. case 2:
  27. animation.translate(-translateUnit, translateUnit).step()
  28. break
  29. case 3:
  30. animation.translate(translateUnit, 0).step()
  31. break
  32. case 4:
  33. animation.translate(0, 0).step()
  34. break
  35. case 5:
  36. animation.translate(-translateUnit, 0).step()
  37. break
  38. case 6:
  39. animation.translate(translateUnit, -translateUnit).step()
  40. break
  41. case 7:
  42. animation.translate(0, -translateUnit).step()
  43. break
  44. case 8:
  45. animation.translate(-translateUnit, -translateUnit).step()
  46. break
  47. }
  48. item.animationData = animation.export()
  49. })
  50. this.cardData = curCardData
  51. this.$apply()
  52. },

算法后面需要优化,我们先完成功能效果,

第三步:卡牌翻转

dom代码


  1. {{cardItem.front}}
  2. {{cardItem.back}}

script代码


  1. data中定义一个curIndex = -1的对象
  2. data = {
  3. curOpen: -1,
  4. }
  5. methods = {
  6. // 抽奖
  7. itemChage (item, curIndex) {
  8. this.cardData[curIndex].front = 'iphone x'
  9. console.log(item, curIndex)
  10. this.curOpen = curIndex
  11. }
  12. }

less


  1. .card.getprize{
  2. .front{
  3. z-index:2;
  4. transform: rotateY(0deg);
  5. }
  6. .back{
  7. z-index:1;
  8. transform: rotateY(180deg);
  9. }
  10. }

现在我们就已经完成了基本的需求;但是在位移动画时候代码写的太丑陋了。
我们来想想怎么优化算法;
我们现在就九宫格布局,我们可以看成是二维布局

那我们是不是可以在卡牌中也使用二维数组布局属性


  1. resetData () {
  2. const total = 9 // 总数
  3. const lineTotal = 3 // 单行数
  4. curCardData.map((item, index) => {
  5. let curCardData = this.cardData
  6. let x = index % lineTotal
  7. let y = parseInt(index / lineTotal)
  8. item.twoArry = {x, y}
  9. })
  10. }

在位移动画中使用二维布局的差值进行位移


  1. // 洗牌
  2. shuffle (translateUnit) {
  3. let curCardData = this.cardData
  4. curCardData.map((item, index) => {
  5. let animation = wepy.createAnimation({
  6. duration: 500,
  7. timingFunction: 'ease'
  8. })
  9. animation.export()
  10. const translateUnitX = translateUnit * (1 - item.twoArry.x)
  11. const translateUnitY = translateUnit * (1 - item.twoArry.y)
  12. animation.translate(translateUnitX, translateUnitY).step()
  13. item.animationData = animation.export()
  14. })
  15. this.cardData = curCardData
  16. this.$apply()
  17. },

这样整个动画就算完成了,

重磅推荐:小程序开店目录

第一部分:小商店是什么

第二部分:如何开通一个小商店

第三部分:如何登录小商店

第四部分:开店任务常见问题

第五部分:小商店可以卖什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收货/物流

第九部分:小程序怎么结算

第十部分:小程序客服

第十一部分:电商创业

第十二部分:小程序游戏开发

电话咨询 微信咨询 预约演示 0元开店