get_set_sound.script 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. function init(self)
  2. msg.post(".", "acquire_input_focus")
  3. -- animate the spaceship up and down "for dramatic effect"
  4. go.animate(".", "position.y", go.PLAYBACK_LOOP_PINGPONG, go.get_position().y + 20, go.EASING_INOUTQUAD, 1)
  5. -- play the engine sound
  6. sound.play("#enginesound")
  7. end
  8. function on_input(self, action_id, action)
  9. if action_id == hash("mouse_button_left") and action.pressed then
  10. -- a list of sounds to chose between
  11. local sounds = {
  12. "/sounds/spaceEngine_001.ogg",
  13. "/sounds/spaceEngine_002.ogg",
  14. "/sounds/spaceEngine_003.ogg",
  15. }
  16. -- pick one at random
  17. local random_sound = sounds[math.random(1, #sounds)]
  18. -- load the new sound
  19. -- stop the currently playing sound
  20. -- set the sound on the sound component
  21. -- play it again
  22. local engine3 = sys.load_resource(random_sound)
  23. sound.stop("#enginesound")
  24. resource.set_sound(go.get("#enginesound", "sound"), engine3)
  25. sound.play("#enginesound")
  26. end
  27. end