views:

31

answers:

2

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.

A: 

what exactly do you mean by "prototyping"? do you mean adding custom methods to a classes prototyp object at runtime? although still possible, that is no longer common practice in AS3 for the following reasons:

  1. not type-safe
  2. bad performance
  3. rarely has any advantage over subclassing.

also, you should pick one method signature for addItem. you listed 3. that's a little confusing. ;)

greetz

back2dos

back2dos
I am sorry, This example incororated a change and I forgot to change the value:Object to url:String.The SoundManager class will keep an object containing all manner of items in an array. I would love to go soundManager.item['12'].play() or soundManager.item['menu'].stop() and also soundManager.item['kill'] would return my object. Would I just add an anonymous function to my object within my array?
Glycerine
A: 

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()
}
shortstick
I had the same thought, however, I'd love to extend this idea from just `play()` to a `stop()` and other commands `loop = true`
Glycerine
so i would then use the second option. make a Sound class that is able to handle your inputs either by throwing events to the main soundManager and having that deal with functionality, or setting reference values for the soundmanager to read: public function addItem(newSound:CustomSound):CustomSound{ array.push(newSound); newSound.addEventListener("start playing", playThis); return newSound;} in the CustomSound: public function play(){ dispatchEvent(new Event("start playing"))}. etc.
shortstick