sound_text_content.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. protected start(): void {
  15. this.node.addComponent(AudioSource)
  16. }
  17. public isCanClose(){
  18. if(this.mData.is_open_click_close==undefined){
  19. this.mData.is_open_click_close = true;
  20. return true;
  21. }
  22. return this.mData.is_open_click_close;
  23. }
  24. public show(widget_id,data:att_text_sound_data,ac:AudioClip){
  25. this.node.on(Node.EventType.TOUCH_START,()=>{
  26. if(this.isCanClose()){
  27. this.node.off(Node.EventType.TOUCH_START)
  28. this.onAudioEnded()
  29. }
  30. })
  31. this.mWidgetId = widget_id;
  32. this.mData = data;
  33. this.m_audio_clip = ac;
  34. this.lab_text.getComponent(Label).string = this.mData.text;
  35. this.item_node.position = new Vec3(this.item_node.position.x,this.mData.pos_y)
  36. if(gameManager.Singleton.getLevelData()){
  37. if(gameManager.getUserData().isOpenPeiYin){
  38. this.onPlaySound()
  39. }
  40. }else{
  41. this.onPlaySound()
  42. }
  43. }
  44. getAudioSource(){
  45. if(this.node.getComponent(AudioSource)==null){
  46. this.node.addComponent(AudioSource)
  47. }
  48. return this.node.getComponent(AudioSource)
  49. }
  50. onPlaySound(){
  51. this.getAudioSource().stop()
  52. this.node.off(AudioSource.EventType.STARTED);
  53. this.node.off(AudioSource.EventType.ENDED);
  54. this.node.on(AudioSource.EventType.STARTED, this.onAudioStarted, this);
  55. // Register the ended event callback
  56. this.node.on(AudioSource.EventType.ENDED, this.onAudioEnded, this);
  57. if (this.m_audio_clip instanceof AudioClip) {
  58. this.getAudioSource().clip = this.m_audio_clip;
  59. this.getAudioSource().play();
  60. this.getAudioSource().volume = 1;
  61. }
  62. }
  63. onAudioStarted(){
  64. }
  65. onAudioEnded(){
  66. this.node.off(AudioSource.EventType.STARTED);
  67. this.node.off(AudioSource.EventType.ENDED);
  68. this.node.active = false;
  69. ClientEvent.dispatchEvent(config.EventRun.WIDGET_FINISH,this.mWidgetId)
  70. }
  71. }