salahzar

PlaySoundWhenClicked.js

Nov 14th, 2021 (edited)
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.  
  3.     var soundURL = null;//SoundCache.getSound(SOUND_URL);
  4.     var lastSoundURL = null;
  5.     var soundVolume = null;
  6.     var soundData = null;
  7.     var injector = null;
  8.     var properties = null;
  9.  
  10.  
  11.     this.clickDownOnEntity = function (entityID, mouseEvent) {
  12.         print("clicked by Salahzar");
  13.         properties = Entities.getEntityProperties(entityID, ["position", "userData"]);
  14.         if (!properties.userData) {
  15.             print("Click Sound emitter " + entityID + " missing user data.");
  16.             return;
  17.         } try {
  18.             soundData = JSON.parse(properties.userData);
  19.             print("SoundURL " + soundData.soundURL);
  20.             // need to check that all this stuff even exists and throw error if not.s
  21.             soundURL = SoundCache.getSound(soundData.soundURL);
  22.             receiverName = soundData.receiverName;
  23.             soundVolume = !isNaN(soundData.soundVolume) ? Number(soundData.soundVolume) : 0.0;
  24.             soundLoop = soundData.isLoop;
  25.             soundLocal = soundData.isLocal;
  26.         } catch (e) { }
  27.  
  28.  
  29.         injector = Audio.playSound(soundURL, {
  30.             position: properties.position,
  31.             volume: soundVolume,
  32.             loop: false,//soundLoop,
  33.             localOnly: true//;//soundLocal
  34.         });
  35.  
  36.     };
  37.  
  38.     this.mousePressOnEntity = this.clickDownOnEntity;
  39.  
  40.     this.unload = function () {
  41.         if (injector) {
  42.             injector.stop();
  43.             injector = null;
  44.         }
  45.     };
  46.  
  47. });
Add Comment
Please, Sign In to add comment