hitEffect.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <view>
  3. <view v-if="bPlay" class="HitEffect-parent">
  4. <view class="HitEffect-container" :style="{'animation-duration': 0+'s'}">
  5. <view class="HitEffect" :id="1" :style="{
  6. '--time':1,
  7. backgroundImage:`url(${imageSrc})`}"
  8. :class=" bPlay?' play ':''">
  9. </view>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import imageSrc from "@/static/effectHit/hitEffect.png"
  16. export default {
  17. props: {
  18. },
  19. data() {
  20. return {
  21. imageSrc: imageSrc,
  22. bPlay:false,
  23. hitTimeOut:null,
  24. hitAudioContext:null,
  25. myHit: "/static/effectHit/pkHit.mp3",
  26. aiHit: "/static/effectHit/pkStricken.mp3",
  27. };
  28. },
  29. created() {
  30. this.hitAudioContext = uni.createInnerAudioContext();
  31. this.hitAudioContext.autoplay = false;
  32. this.hitAudioContext.src = this.myHit;
  33. },
  34. beforeDestroy() {
  35. if (this.hitAudioContext)
  36. this.hitAudioContext.destroy();
  37. },
  38. methods: {
  39. onAIHit(){
  40. this.hitAudioContext.stop();
  41. this.hitAudioContext.volume = 0.8;
  42. this.hitAudioContext.src = this.aiHit;
  43. this.hitAudioContext.play();
  44. },
  45. onMyHit(){
  46. this.hitAudioContext.stop();
  47. this.hitAudioContext.volume = 0.4;
  48. this.hitAudioContext.src = this.myHit;
  49. this.hitAudioContext.play();
  50. },
  51. onPlay(){
  52. this.bPlay = true;
  53. if(this.hitTimeOut){
  54. clearTimeout(this.hitTimeOut);
  55. this.hitTimeOut = null;
  56. }
  57. this.hitTimeOut = setTimeout(()=>{
  58. this.bPlay = false;
  59. },1000);
  60. },
  61. onStop(){
  62. this.bPlay = false;
  63. if(this.hitTimeOut){
  64. clearTimeout(this.hitTimeOut);
  65. this.hitTimeOut = null;
  66. }
  67. }
  68. }
  69. }
  70. </script>
  71. <style lang="scss">
  72. .HitEffect-parent {
  73. // border: 1rpx solid #ffaa7f;
  74. position: absolute;
  75. display: flex;
  76. justify-content: center;
  77. overflow: hidden;
  78. top: -50px;
  79. left: -35px;
  80. transform: scale(0.5);
  81. }
  82. .HitEffect-container {
  83. width: 100%;
  84. height: 167px;
  85. // border: 1rpx solid #000000;
  86. position: relative;
  87. }
  88. .HitEffect {
  89. // background-color: #007AFF;
  90. background-size: cover;
  91. height: 167px;
  92. width: 146px;
  93. position: relative;
  94. top: 0;
  95. left: 0;
  96. }
  97. .play {
  98. animation: hitEffectRun calc(var(--time) * 1s) steps(1, start) infinite;
  99. animation-play-state: running;
  100. }
  101. @keyframes hitEffectRun {
  102. @for $i from 0 through 9 {
  103. #{$i*11.1}% {
  104. background-position: -0px -167px*$i;
  105. }
  106. }
  107. }
  108. </style>