Search code examples
actionscript-3audiofunction-prototypes

prototype functions off custom functions


Its a complicated senario for me.

I have a sound management singleton with an asset like dictionary storing all referances to my urls and assets and the guff inside it-

I have a function called addItem(id:String, url:String):Object

I would love to do something similar as soundManager.addItem(id:String, url:String).play() or soundManager.addItem(id:String, url:String).stop() of which it'll both add my item to my dictionary, and begin playing the sound

Currently I do soundManager.addItem(id:String, url:String) then soundManager.play('myID').

My sound asset is an object containing a few bits like Sound, SoundChannel, SoundTransform, URL and some other none descript parts.

I know it will be prototyping - I just get uber stuck when I need to prototype my custom functions and objects.

Thanks in advance


conclusion:

Well, I did do the autoplay as mentioned in my accepted answer.

I also did something pretty cool which I like the look of.

I create a SoundManager class, of which handles and uses a SoundObject of which contains SoundTransform, SoundChannel and anything else Sound object requires.

This way when I add an item to the SoundManager, it'll always return the SoundObject Class that incorporates all the play(), pause(), volume(), position() I need.

Its really very useful and I've already used it on 4 projects! Yey.

Thanks guys for your help.


Solution

  • if you are looking at the code then:

    soundManager.addItem(url:String)
    

    is returning an object (which you just added), which is then being given the play() command. you have a few easy options in this.

    1) if you always play the sound, then you just add the command into the addItem() function.

    2) if you want to do exactly what you have there then you need to make a proper class for the sound object with a play() function. probably one that dispatches an event to soundManager that then switches the sound.

    3) add a boolean to the add statement that is an autoplay function, something like:

    addItem(value:*, autoPlay:Boolean = false):Object{
    if(autoplay) play()
    }