sound_text_content.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { _decorator, AudioClip, AudioSource, Component, Label, Node, Vec3 } from 'cc';
  2. import { att_text_sound_data, event_item } from '../../../data/data';
  3. import { gameManager } from '../gameManager';
  4. import { ClientEvent } from '../../clientEvent';
  5. import { config } from '../../config';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('sound_text_content')
  8. export class sound_text_content extends Component {
  9. @property(Node) lab_text:Node = null;
  10. @property(Node) item_node:Node = null;
  11. private m_audio_clip:AudioClip = null;
  12. private mData:att_text_sound_data = null;
  13. private mWidgetId:number = 0;
  14. private mEventItem:event_item = null;
  15. protected start(): void {
  16. this.node.addComponent(AudioSource)
  17. }
  18. public isCanClose(){
  19. if(this.mData.is_open_click_close==undefined){
  20. this.mData.is_open_click_close = true;
  21. return true;
  22. }
  23. return this.mData.is_open_click_close;
  24. }
  25. public initEventItem(event_item:event_item) {
  26. this.mEventItem = event_item
  27. }
  28. public show(widget_id,data:att_text_sound_data,ac:AudioClip){
  29. this.node.on(Node.EventType.TOUCH_START,()=>{
  30. if(this.isCanClose()){
  31. this.node.off(Node.EventType.TOUCH_START)
  32. this.onAudioEnded()
  33. }
  34. })
  35. this.mWidgetId = widget_id;
  36. this.mData = data;
  37. this.m_audio_clip = ac;
  38. this.lab_text.getComponent(Label).string = this.mData.text;
  39. this.item_node.position = new Vec3(this.item_node.position.x,this.mData.pos_y)
  40. if(gameManager.Singleton.getLevelData()){
  41. if(gameManager.getUserData().isOpenPeiYin){
  42. this.onPlaySound()
  43. }
  44. }else{
  45. this.onPlaySound()
  46. }
  47. }
  48. getAudioSource(){
  49. if(this.node.getComponent(AudioSource)==null){
  50. this.node.addComponent(AudioSource)
  51. }
  52. return this.node.getComponent(AudioSource)
  53. }
  54. onPlaySound(){
  55. this.getAudioSource().stop()
  56. this.node.off(AudioSource.EventType.STARTED);
  57. this.node.off(AudioSource.EventType.ENDED);
  58. this.node.on(AudioSource.EventType.STARTED, this.onAudioStarted, this);
  59. // Register the ended event callback
  60. this.node.on(AudioSource.EventType.ENDED, this.onAudioEnded, this);
  61. if (this.m_audio_clip instanceof AudioClip) {
  62. this.getAudioSource().clip = this.m_audio_clip;
  63. this.getAudioSource().play();
  64. this.getAudioSource().volume = 1;
  65. }
  66. }
  67. onAudioStarted(){
  68. }
  69. onAudioEnded(isDispatchEvent:boolean=true){
  70. this.node.off(AudioSource.EventType.STARTED);
  71. this.node.off(AudioSource.EventType.ENDED);
  72. this.node.active = false;
  73. if(isDispatchEvent) {
  74. ClientEvent.dispatchEvent(config.EventRun.WIDGET_FINISH,this.mWidgetId,this.mEventItem)
  75. }
  76. }
  77. }