Skip to content

Instantly share code, notes, and snippets.

@raldred
Last active December 25, 2015 13:19
Show Gist options
  • Save raldred/6982954 to your computer and use it in GitHub Desktop.
Save raldred/6982954 to your computer and use it in GitHub Desktop.
SoundManager for IGE using createjs's soundJS and TweenMax for music fading Grab the instance of the soundmanager class with the following `@sound_manager = SoundManager.getInstance()`
SoundManager = IgeClass.extend
classId: 'SoundManager'
init: ->
@log 'Initializing'
@volumes = {
music: .6
fx: .6
}
@music_instance = null
@effects_instances = {}
@current_music = ""
@paused = false
@muted = false
registerMusic: (path,id) ->
createjs.Sound.registerSound(path,"music-#{id}")
playMusic: (id) ->
# return if id == @current_music
unless @music_instance
@log 'creating music instance'
@music_instance = createjs.Sound.createInstance "music-#{id}"
@log 'going to play music', id
if @music_instance.playState == createjs.Sound.PLAY_SUCCEEDED
@_fadeBetweenMusic id
else
@music_instance.play createjs.Sound.INTERRUPT_NONE,0,0,-1,SoundManager.MUSIC_VOLUME,0
@music_instance.pause() if @paused
@current_music = id
registerFX: (path,id) ->
createjs.Sound.registerSound(path,"fx-#{id}")
playFX: (id) ->
@log 'going to play fx', id
instance = createjs.Sound.createInstance "fx-#{id}"
@effects_instances[instance.uniqueid] = instance
instance.addEventListener "complete", (e) =>
delete @effects_instances[instance.uniqueid]
instance.play createjs.Sound.INTERRUPT_NONE,0,0,0,SoundManager.FX_VOLUME,0
pauseAll: ->
@music_instance.pause() if @music_instance and @music_instance.playState == createjs.Sound.PLAY_SUCCEEDED
for i in @effects_instances
i.pause() if i.playState == createjs.Sound.PLAY_SUCCEEDED
@paused = true
resumeAll: ->
return if @muted
@music_instance.resume() if @music_instance.playState == createjs.Sound.PLAY_SUCCEEDED
for i in @effects_instances
i.resume() if i.playState == createjs.Sound.PLAY_SUCCEEDED
@paused = false
mute: ->
@muted = true
@pauseAll()
createjs.Sound.setMute(true)
unmute: ->
@muted = false
@resumeAll()
createjs.Sound.setMute(false)
_fadeBetweenMusic: (id) ->
@log 'fading between music'
# fade out the current music
prev_instance = @music_instance
prev_instance.volume = prev_instance.getVolume()
TweenMax.to prev_instance, .4,
volume:0
onUpdateParams: [prev_instance]
onUpdate: @_onSetVol
onCompleteParams: [prev_instance]
onComplete: (i) =>
i.stop()
# now do work with the music to be played
next_instance = createjs.Sound.createInstance "music-#{id}"
next_instance.volume = 0
next_instance.play createjs.Sound.INTERRUPT_NONE,0,0,-1,0,0
next_instance.pause() if @paused
TweenMax.to next_instance, 1,
volume: @volumes.music
onUpdateParams: [next_instance]
onUpdate: @_onSetVol
@music_instance = next_instance
_onSetVol: (instance) =>
instance.setVolume(instance.volume)
SoundManager.getInstance = ->
@instance ||= new SoundManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment